qt - QML,如何从另一个 qml 访问元素

标签 qt qml

我想问简单的问题以获得快速解决方案。我在 ma​​in.qml 中有 ListViewButton ,在 openingtaskdelage.qml 中有 Component 。这是文件ma​​in.qml:

ListView
{
    id: grid

    delegate:mydelegate


    model: mymodel
    spacing: 5
    orientation:ListView.Vertical
    Layout.preferredWidth: width_commmon
    Layout.preferredHeight: height_body
    Layout.maximumHeight: 800
    Layout.alignment: Qt.AlignHCenter
    // highlightFollowsCurrentItem: false
    focus: true

    ClosingTaskModel
    {
        id:mymodel
    }

    ClosingTaskDelegate
    {
        id:mydelegate
    }


}

Button
{
    id:finishbutton
    Layout.alignment: Qt.AlignHCenter
    Layout.preferredWidth: width_commmon
    Layout.preferredHeight:height_endbutton
    text: "İş Emrini Tamamla"
    onClicked:
    {
         // console.log((grid.children["border1"]).id);
    }
}

和文件ClosingTaskDelegate.qml:

Component
{
    id:component1

    Rectangle
    {
        signal sendMenuValuestoJS
        height: 200
        width: 200

        Text
        {
            id:text1;
            text:header;
            height:parent.height/2
            //    width: parent.width

            anchors.horizontalCenter:parent.horizontalCenter;
            font.pointSize:20;
            color:"red";
            //  height:parent.height/2
            //  width: parent.width
        }
    }
}

不要在意细节。我的主要问题是:当 ma​​in.qml 中的 onClickButton() 发生时,必须触发 signal sendMenuValuestoJS() 。该信号必须发送字符串值作为参数(即发送文本)。 我无法从 ma​​in.qml 访问信号。我已在 Component 中定义了 signal,但出现以下运行时 QML 错误:

Component objects cannot declare new signals.

如何信号连接到某些功能

最佳答案

要使用什么Connections

这是这个想法,

/* main.qml */
Rectangle {
    width: 200
    height: 200

    MouseArea {
        id: mouse
        anchors.fill: parent
    }

    Click {
        id: click
        targetto: mouse
    }
}

/* Click.qml */
Item {
    property variant targetto
    Connections {
        target: targetto
        onClicked: console.log("CLICKED!");
    }
}

关于qt - QML,如何从另一个 qml 访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34180748/

相关文章:

c++ - 如何在 QML 文件中使用属性名称作为字符串查找 QML 上下文属性

c++ - Qt:如何调整图像大小并保持其比例?

qt - 如何找到所有 QML 模块的最新版本

qt - 什么是 SIGSEGV,Qt 中的段错误

c++ - 自定义QGraphicsPixmapItem不显示像素图

c++ - QML 应用程序崩溃;如何调试?

c++ - Qt Qml 连接到上下文属性的 QObject 属性的信号

qt - 自定义 URL 协议(protocol)处理程序 Qt 5

c++ - 从 QList 中删除重复的对象

c++ - 使用 QT + OpenCV 的多线程