Solved Qml accessing ui object.
-
Hello everyone ,
I decided recently to learn Qml to upgrade my app design .
(I'm new with Qt in general)However i have some issues with Qml and cannot find the solution to my problem , i think that i didn't understand well the logic behind accessing object in Qml.
The task is simple i have a label and a button whenever i click the button i want the text on the label to change .
I read the Qt documentation saw tutorials , and read a post on the forum where someone wanted to access an object but this is still not working .Here is a sample of my code and thanks to everyone who will help me !
//myfile.ui property alias partSelectionLabel: partSelectionLabel property alias part1Button: part1Button Label { id: partSelectionLabel ..... } Button { id: part1Button ... } //myfile.qml part1Button.onClicked: { partSelectionLabel.text = "Part 1 Selected" }
-
Hi
Im not in QML but in case you didn't stumble over
https://qmlbook.github.io/ -
I saw this one and i tried to copy their code logic but still my code isn't working.
-
You dont get any errors clicking the button ?
-
no nothing it is just not working my label stay the same
-
@petitDoigtPuissant
Try withpart1Button.onClicked: {
console.log("Button Pressed: ");
}Project loads and run, correct ?
Its just that nothing happens when button is clicked ?Im not sure if you need a mouse area.
https://stackoverflow.com/questions/32893811/creating-a-custom-qml-button-with-native-look-and-feelAnyway. give it some time , other user can tell this easy.
-
I tried the console.log before.
i just tried again with your code and there is nothing in the console this is so weird because it would say that it is not even entering the "function".yes yes the projects loads and run with no error i have my window my label and my buttons but nothing is happening.
I will take a look, but if its a button i should not need it i think.
Yes no worry i'm stuck with this since 4 days so one more day won't hurt. -
@petitDoigtPuissant It's difficult to say if your code is well-formed because you may have cut off some of it. Please post the whole files if they aren't too large. If these were the whole files they can't work.
And name the files which contain your components/types beginning with an uppercase letter. Type names begin with upper case, property names with lower case.
-
//SettingPageForm.ui.qml import QtQuick 2.4 import QtQuick.Controls 2.2 import QtQuick.Controls.Material 2.0 import QtQuick.Layouts 1.0 PageBackground { id: pageBackground property alias part2Button: part2Button property alias part1Button: part1Button property alias partSelectionLabel: partSelectionLabel RowLayout { y: 318 spacing: 20 anchors.left: parent.left anchors.leftMargin: 333 Button { id: part1Button text: qsTr("Part 1") Layout.preferredHeight: 60 Layout.preferredWidth: 142 highlighted: true } Button { id: part2Button text: qsTr("Part 2") Layout.preferredHeight: 60 Layout.preferredWidth: 142 highlighted: true } } Text { id: partSelectionLabel x: 370 width: 252 height: 46 text: qsTr("Select a ship part") fontSizeMode: Text.HorizontalFit anchors.top: parent.top anchors.topMargin: 150 font.bold: true font.capitalization: Font.AllUppercase horizontalAlignment: Text.AlignHCenter font.pixelSize: 22 } }
-
//SettingPage.qml import QtQuick 2.4 SettingPageForm { part1Button.onClicked: { console.log("Button pressed: "); partSelectionLabel.text = "Part 1 Selected"; } // part2Button.onClicked: { // partSelectionLabel.text = "Part 2 Selected"; // } }
these are the files concerned . To create the signal on the button i did just like in a QtStudio video on Youtube right click on the button in the ui and go to implementation. But it seems like the button is not responding because even the console.log i put doesn't appear
-
ok so guys sorry i just found my error .
It was very very stupid i wasn't on release mode but in debug .
I got it when i thought about why nothing was printing into the console when i was putting console.log.
Everything is working now !anyway thank you !