我想要做的是創建一個單獨的 QML 檔案來處理呼叫信號,只是為了清潔。
我有:
Signal.qml(處理信號的檔案)
Content.qml(包含所有組件的 UI 檔案)
Main.qml(包含視窗的主 QML 檔案Content.qml)
我正在嘗試從信號函式內部修改標簽文本,如下所示Content.qml: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
}
}
uj5u.com熱心網友回復:
您應該從 Signal 檔案中參考 Content ( ) 的實體(而不是type):programContent
import email 1.0
import "qrc:/ui/qml/component"
Email {
property var theContent
onEmailListIndex: function(param1) {
theContent.progressBarLabelText = "testing"
}
}
然后從 Window.qml 提供它:
Content {
id: programContent
}
Signal{
id: programSignals
theContent: programContent
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/490781.html
標籤:javascript qt qml qt5
