QML在另一个QML文件中从JS函数中修改属性别名
我要做的是创建一个单独的QML文件,以处理名为“信号”,只是为了清洁。
我有:
signal.qml
(处理信号的文件)
content.qml
(所有组件的UI文件)
main.qml
包含content.qml
的窗口。
在content.qml
中, code.cml
in signal> signal> signal.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
:
and then the signal function that needs to change label text:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
信号文件(而不是 type )中引用内容(
programContent
)的实例:您应该从 :
You should reference the instance of Content (
programContent
) from the Signal file (instead of the type):and then from Window.qml supply it: