Set dynamic text via function
-
Ok that's kinda weird (but perhaps it's another Qt6 Bug?)
Currently I try to change the text of this Text Element. And I'm really stuck :/
It's
Text{ id: welcomeText anchors.top: chargingBackground.bottom anchors.horizontalCenter: chargingBackground.horizontalCenter anchors.topMargin: 20 text: qsTr("Please select your charging pod!") property alias infoText: welcomeText.text font.pointSize: 20 font.family: "Helvetica" } function slotSelected(id) { if(!hpcManager.chargerIsOccupied()) { hpcManager.setCurrentCharger(id) t.running = false } else { console.log("No more parking spaces available!"); welcomeText.infoText = "No more parking spaces available!"; } }
all in "root" space.
It all works fine. The Console logs properly... but the text doesn't change. What am I missing here?
I tried to apply the alias at different levels (because I found it written that way in other solutions) but I can't make it work. -
Code looks correct. Perhaps it is a bug, like you say. Can you test with Qt 5.15?
-
Jepp. Need to download it first, but I'll try.
-
@Meistermosher it has to be somewhere else in your code, this basic example works just fine:
import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 1.4 Window { visible: true width: 640 height: 480 Button{ id:btn anchors{ left: parent.left right: parent.right top: parent.top } height: 50 text: "Change text" onClicked: slotSelected(1) } Text{ id: welcomeText anchors.top: btn.bottom anchors.horizontalCenter: btn.horizontalCenter anchors.topMargin: 20 text: qsTr("Please select your charging pod!") property alias infoText: welcomeText.text font.pointSize: 20 font.family: "Helvetica" } function slotSelected(id) { console.log("slotSelected", id) console.log("No more parking spaces available!"); welcomeText.infoText = "No more parking spaces available!"; } }
-
Damn. I was sure, I figured it out the right way but encountered a bug. So please, what have I messed up here?
import QtQuick 2.12 import QtQuick.Controls 2.12 //import de.martingebske Item { id: wsRoot Image{ id: background source: "qrc:/Assets/Img/background_2.png" height: 768 width: 1024 anchors.fill: parent } Rectangle{ id: whiteBack color: "white" opacity: 0.1 anchors.horizontalCenter: parent.horizontalCenter anchors.top: parent.top anchors.topMargin: 100 height: 300 width: 700 } // Charger Selection Rectangle { id: chargingBackground anchors.fill: whiteBack color: "transparent" Row{ id: chRow anchors.fill: parent spacing: 20 topPadding: 20 leftPadding: 30 ChargingSlot{ id: ccs1 source: "qrc:/Assets/Img/ccs_logo.png" kw: "300kW" onClicked: slotSelected(1) } ChargingSlot{ id: ccs2 source: "qrc:/Assets/Img/ccs_logo.png" kw: "300kW" onClicked: slotSelected(2) } ChargingSlot{ id: ac source: "qrc:/Assets/Img/ac_logo.png" itemHeight: 150 itemWidth: 150 kw: "42kW" textMargin: -120 onClicked: slotSelected(3) } } } Text{ id: welcomeText color: "white" anchors.top: chargingBackground.bottom anchors.horizontalCenter: chargingBackground.horizontalCenter anchors.topMargin: 20 text: qsTr("Please select your charging pod!") property alias infoText: welcomeText.text font.pointSize: 20 font.family: "Helvetica" } function slotSelected(id) { if(!hpcManager.chargerIsOccupied()) { hpcManager.setCurrentCharger(id) t.running = false } else { welcomeText.infoText = "No more parking spaces available!"; console.log("No more parking spaces available!"); } } Rectangle{ id: footer height: 50 width: wsRoot.width anchors.bottom: wsRoot.bottom color: "transparent" Column{ anchors.verticalCenter: parent.verticalCenter leftPadding: 30 Text{ id: date color: "white" font.pointSize: 10 text: "Datum: 23.01.1992" } Text { id: time color: "white" font.pointSize: 10 text: "Uhrzeit: 22:22:01 Uhr" } } Row{ anchors.right: parent.right height: parent.height rightPadding: 30 spacing: 40 Rectangle{ id: lanBtn height: 35 width: 80 color: "transparent" border.color: "white" Image { anchors.verticalCenter: parent.verticalCenter source: "/Assets/Img/language_icon.png" anchors.centerIn: parent width: 30 fillMode: Image.PreserveAspectFit } MouseArea{ anchors.fill: parent onClicked: {languagePopup.open(); t.running = false} } } Rectangle{ id: helpBtn height: 35 width: 80 color: "transparent" border.color: "white" Text{ anchors.verticalCenter: parent.verticalCenter anchors.centerIn: parent font.family: "Helvetica" text: qsTr("Help") font.pointSize: 12 color: "white" } MouseArea{ anchors.fill: parent onClicked: {console.log("Help Select!"); t.running = false} } } } } Popup{ id: languagePopup anchors.centerIn: parent width: 600 height: 200 modal: true focus: true closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside ImageButton{ } Button { id: closeButton anchors.bottom: parent.bottom anchors.horizontalCenter: parent.horizontalCenter text: "Close" onClicked: languagePopup.close() } } LoadManager{ id:l } Timer{ id: t interval: 5000 running: true onTriggered: lm.source = "IdleScreen.qml" } }
-
Provide a minimal reproducible example of your problem if you want help.
It shouldn't need any outside dependencies (here I see ChargingSlot, LoadManager, IdleScreen.qml, the different images), fit in a single file if possible and be the most minimal as possible.
By doing this you'll most likely figure out the issue by yourself too. -
@GrecKo Ok, it was kinda connected to my Custom Image Button which ... I think overwrites the clicked function.
import QtQuick 2.0 import de.martingebske Rectangle{ id: root height: 200 width: 200 color: "transparent" property string kw: "" property string source: "" property double itemHeight: 200 property double itemWidth: 200 property double textMargin: -70 signal clicked() Image { id: img source: root.source height: root.itemHeight width: root.itemWidth Text{ id: txt color: "white" anchors.bottom: img.bottom anchors.horizontalCenter: img.horizontalCenter anchors.bottomMargin: root.textMargin text: root.kw font.pointSize: 20 font.family: "Helvetica" } MouseArea{ id: ma anchors.fill: img enabled: true onClicked: { if(!hpcManager.chargerIsOccupied()){ root.clicked() img.opacity = 0.2; txt.opacity = 0.6; ma.enabled = false } else console.log("No more parking spaces available!") } } } }
When I use regular Buttons instead of my custom buttons it works as intended.
Still I don't really get it, why the text doesn't change (because it's absolutely not related to the custom Button) but I think it's something in the MouseArea click event..
just a wild guess...