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

发布于 2025-02-07 08:29:49 字数 6590 浏览 2 评论 0原文

我要做的是创建一个单独的QML文件,以处理名为“信号”,只是为了清洁。

我有:

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

content.qml(所有组件的UI文件)

main.qml 包含content.qml的窗口。

content.qml中, code.cml in signal> signal> signal.qml在信号的内部 函数如图所示:

content.qml中的属性别名:

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

输出没有错误,只是标签不会像应有的那样更改。

信号:

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
    }
}

内容:

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
            }
        }

    }
}

主要:

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
    }
}

what I'm trying to do is create a separate QML file to handle called signals just for cleanliness sake.

I have:

Signal.qml (file that handles signals)

Content.qml (UI file with all components)

Main.qml (main QML file that houses the window for Content.qml)

I'm attempting to modify label text from Content.qml within Signal.qml inside of the signal function as shown:

property alias within Content.qml:
enter image description here

and then the signal function that needs to change label text:
enter image description here

There is no errors on output, just the label doesn't change like it should.

Signal:

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
    }
}

Content:

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:

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2025-02-14 08:29:49

信号文件(而不是 type )中引用内容(programContent)的实例

import email 1.0
import "qrc:/ui/qml/component"

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

}

您应该从 :

Content {
    id: programContent
}
Signal{
    id: programSignals
    theContent: programContent
}

You should reference the instance of Content (programContent) from the Signal file (instead of the type):

import email 1.0
import "qrc:/ui/qml/component"

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

}

and then from Window.qml supply it:

Content {
    id: programContent
}
Signal{
    id: programSignals
    theContent: programContent
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文