首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从另一个QML文件中的JS函数修改属性别名

从另一个QML文件中的JS函数修改属性别名
EN

Stack Overflow用户
提问于 2022-06-14 17:08:36
回答 1查看 102关注 0票数 1

我想要做的是创建一个单独的QML文件来处理被调用的信号,这仅仅是为了清洁。

我有:

Signal.qml (处理信号的文件)

Content.qml (包含所有组件的UI文件)

Main.qml (包含Content.qml窗口的主QML文件)

我试图在信号函数内部的Content.qml中修改Signal.qml中的标签文本,如下所示:

Content.qml中的属性别名:

然后,需要更改标签文本的信号函数:

输出没有错误,只是标签没有改变。

信号:

代码语言:javascript
运行
复制
import email 1.0
import "qrc:/ui/qml/component"
Email {
    onEmailListIndex: function(param1) {
        Content.progressBarLabelText = "testing"
        //label.text = qsTr(progressBar.value + " / " + progressBar.to + " Emails Sent")
        //progressBar.value = param1
    }

    onEmailListSize: function(param1) {
        //label.text = qsTr(progressBar.value + " / " + param1 + " Emails Sent")
        //progressBar.to = param1
    }
}

内容:

代码语言:javascript
运行
复制
import QtQuick.Window 2.12
import QtQuick.Controls 2.3
import QtQuick.Controls.Universal 2.15
Page {
    id: page
    width: 700
    height: 700
    anchors.fill: parent

    property alias progressBarLabelText: progressBarLabel.text


    Pane {
        id: mainContentPane
        visible: true
        anchors.fill: parent
        bottomPadding: 0
        horizontalPadding: 0
        padding: 0

        Rectangle {
            id: leftRectangle
            width: 100
            color: "#151515"
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 0
            anchors.topMargin: 0
            anchors.leftMargin: 0

            ScrollView {
                id: leftScrollView
                anchors.fill: parent
                ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
                ScrollBar.vertical.policy: ScrollBar.AlwaysOff
                clip: true


                ItemDelegate {
                    id: statusButton
                    x: 0
                    y: 99
                    width: 100
                    height: 100
                    text: qsTr("Status")
                    highlighted: false
                    padding: 12
                    antialiasing: true
                    layer.smooth: false
                    display: AbstractButton.TextUnderIcon
                    icon.source: "qrc:/ui/icon/flat-screen-monitor.png"
                }

                ItemDelegate {
                    id: emailButton
                    x: 0
                    y: 199
                    width: 100
                    height: 100
                    text: qsTr("Email")
                    layer.smooth: false
                    antialiasing: true
                    display: AbstractButton.TextUnderIcon
                    onPressed: programSignals.workerThread()
                    icon.source: "qrc:/ui/icon/mail.png"
                }

                ItemDelegate {
                    id: settingsButton
                    x: 0
                    y: 300
                    width: 100
                    height: 100
                    text: qsTr("Settings")
                    layer.smooth: false
                    antialiasing: true
                    padding: 12
                    display: AbstractButton.TextUnderIcon
                    icon.source: "qrc:/ui/icon/settings.png"
                }

                ItemDelegate {
                    id: homeButton
                    x: 0
                    y: 0
                    width: 100
                    height: 100
                    text: qsTr("Home")
                    layer.enabled: false
                    layer.smooth: false
                    display: AbstractButton.TextUnderIcon
                    antialiasing: true
                    padding: 12
                    onPressed: statusPane.visible=false
                    icon.source: "qrc:/ui/icon/home.png"
                }
            }
        }
        Rectangle {
            id: rightRectangle
            color: "#000000"
            anchors.left: leftRectangle.left
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.leftMargin: 101
            anchors.bottomMargin: 0
            anchors.topMargin: 0
            anchors.rightMargin: 0


            ScrollView {
                id: statusPane
                anchors.fill: parent
                clip: false
                ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
                ScrollBar.vertical.policy: ScrollBar.AlwaysOff
                ProgressBar {
                    id: emailProgressBar
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.top: parent.top
                    anchors.bottomMargin: 618
                    anchors.leftMargin: 107
                    anchors.topMargin: 664
                    anchors.rightMargin: 107
                    from: 0
                }

                Label {
                    id: progressBarLabel
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.top: parent.top
                    text: "lol"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    anchors.bottomMargin: 592
                    anchors.topMargin: 680
                    anchors.leftMargin: 107
                    anchors.rightMargin: 107
                }

            }

            ScrollView {
                id: homePane
                visible: false
                anchors.fill: parent
            }
        }

    }
}

Main:

代码语言:javascript
运行
复制
import QtQuick.Window 2.15
import QtQuick.Controls.Universal 2.15
import email 1.0
import "component"
import "signal"
Window {
    id: window
    height: 700
    width: 700
    minimumHeight: 700
    minimumWidth: 700
    visible: true
    color: "#000000"
    title: qsTr("Email Program")
    Universal.theme: Universal.Dark
    Universal.accent: Universal.Violet
    Content {
        id: programContent
    }
    Signal{
        id: programSignals
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-14 18:41:54

您应该从信号文件引用内容实例(programContent) (而不是类型):

代码语言:javascript
运行
复制
import email 1.0
import "qrc:/ui/qml/component"

Email {
    property var theContent
    onEmailListIndex: function(param1) {
        theContent.progressBarLabelText = "testing"
    }

}

然后从Window.qml提供它:

代码语言:javascript
运行
复制
Content {
    id: programContent
}
Signal{
    id: programSignals
    theContent: programContent
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72620915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档