【发布时间】:2019-03-13 10:13:40
【问题描述】:
我在使用我的 QML 代码时遇到了一些自定义项目的问题。我创建了一组包含在 RowLayout 中的项目,其中包含一个标签和一个输入控件(例如 SpinBox 或 ComboBox)。为了“将值改变的信号带到外面”,我在外部 RowLayout 中添加了一个应该在 onValueChanged 上发出的信号。代码如下:
import QtQuick 2.10
import QtQuick.Controls 2.2
import QtQuick.Window 2.2
import QtQuick.Controls.Material 2.3
import QtQuick.Layouts 1.0
RowLayout {
property alias label: innerLabel.text
property alias value: innerNum.value
property alias editable: innerNum.editable
property alias min: innerNum.from
property alias max: innerNum.to
property string myPage: ""
property string myMeasure: ""
property int fontSize: 16
property bool signaling: true
property var colorAccent: Material.accent
property var colorPrimary: Material.primary
signal fabValueChanged(var message)
Label{
id: innerLabel
Layout.fillHeight: filling
Layout.fillWidth: filling
Material.foreground: colorAccent
font.pixelSize: fontSize
verticalAlignment: Text.AlignVCenter
property bool filling: true
onTextChanged: function(){
if (text == ""){
filling = false;
width = 0;
}
else{
filling = true;
}
}
}
SpinBox{
id: innerNum
Layout.fillHeight: true
Layout.fillWidth: true
Material.foreground: colorAccent
editable: true
to: 100000
from: 1
value: 1
onValueChanged: function(){
if(parent.signaling){
var toSend = {"name":parent.objectName,"value":value,"measure":parent.myMeasure};
fabValueChanged(toSend);
}
}
}
}
当我将 fabValueChanged 连接到一个仅打印消息的简单插槽时,即使正确发出了 onValueChanged 信号,我什么也没有得到。 会不会是 RowLayout 不能发出信号?如果是这种情况,我该如何修改我的对象以避免这个问题? 如果我的问题不是布局,那是怎么回事?
【问题讨论】:
-
您确定调用了
onValueChanged处理程序吗?尝试在那里添加console.log(...)。但最后我认为你需要连接到onValueModified。 (顺便说一句,你也可以离开function()) -
我建议您检查
onValueChanged()调用的调试器,它调用fabValueChanged()。另外,正如@Amfasis 已经注意到的那样,我会将onEvent: function(){}替换为onEvent: {} -
最后问题不在于元素,而在于我用来动态创建这些对象的函数。耽误了你的时间,我真的很抱歉。
标签: qt qml signals signals-slots