【问题标题】:Can a RowLayout item have signals in QML?QML 中的 RowLayout 项目可以有信号吗?
【发布时间】: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


【解决方案1】:

在这个问题上,我真的很喜欢在别人的时间里。最后问题出在我用来动态创建这些对象的函数上。所以对象本身可以毫无问题地发出信号。

再次抱歉

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多