Avoid unqualified access with required property implementation trouble
-
Hello,
I got a question about QML best practices implementation. My case is simple, opening a dialog when ckicking on a toolbar button.
In the first place I wrote this:main.qml:
ApplicationWindow { readonly property ApplicationWindow appWindow: this width: 640 height: 480 visible: true // ... }
MenuAbout.qml:
Menu { title: qsTr("A propos") MenuItem { text: qsTr("&A Propos") display: AbstractButton.TextOnly onTriggered: aboutDialog.open() } DialogAbout { id: aboutDialog } }
DialogAbout.qml:
Dialog { id: aboutDialog anchors.centerIn: parent parent: appWindow.contentItem modal: false title: "About this" // ... }
This was based on what I've found (on stackoverflow) at the time to solve the "problem" about the need of the Dialog component to have a parent to open in.
So this is what I've done to get a reference to a parent component even if I had concerns about the concept of a global property "floating" arround, accessible to any children.Then I found this video of a talk made during the QtWS 2021. And there is a section named 'Avoid unaqualifed access - Outside property' where I think I've found a better solution.
So as it is done in the video I've modify main.qml:
ApplicationWindow { id: appWindow width: 640 height: 480 visible: true // ... }
and AboutDialog.qml:
Dialog { required property ApplicationWindow appWindow id: aboutDialog anchors.centerIn: parent parent: appWindow.contentItem modal: false title: "About this" // ... }
But in this case the application crash when lauching it with the following error:
"Required property appWindow was not initialized".And that's where I get confused. For me, as 'appWindow' is the id I gave to the application window in the main.qml file it should be initialized when the application start, no?
So I don't know if I got in the wrong way (required property should not be use in this case) or if I just missing a piece.Thanks for reading.
-
Hello @JKSH
Thank you for your reply therefore there still something that I don't understand.
In my main.qml I addedAboutDialog { appWindowRef: appWindow //I've changed the required property's name in AboutDialog to avoid confusion }
But I still get the same error message "Required property appWindowRef was not initialized". I feel that I'm missing something obvious, sorry about that.
And I got an other question in terms of best practices is it ok to "intialize" all the component which will need a reference to the Application Window in the main.qml file ? Or is there a cleaner way ?
Edit: There is something that I've forgot to mention.
I've also tried to initialoze a required property in an other component (just to test) which is also declared in the main.qml and in this case it is working perfectly.main.qml
ApplicationWindow { id: appWindow //... MainStackLayout { appWindowRef: appWindow anchors.top: mainToolBar.bottom anchors.bottom: mainTabBar.top currentIndex: mainTabBar.currentIndex }
MainStackLayout.qml
StackLayout { id: layout required property ApplicationWindow appWindowRef //... }
So is it supposed to be different with Dialog ?
And concerning my question about good pratices, I've look to some projects examples (those available in Qt Creator Examples tab) and it seems to be done that way. So I guess it is the right way at least for someone whith my level of knowledge which is not very high.
-
@Aymeric_Qt said in Avoid unqualified access with required property implementation trouble:
For me, as 'appWindow' is the id I gave to the application window in the main.qml file it should be initialized when the application start, no?
So I don't know if I got in the wrong way (required property should not be use in this case) or if I just missing a piece.If your AboutDialog has a required property called appWindow, that means your main.qml needs to assign something to the dialog's appWindow property:
AboutDialog { appWindow: <something> // This line is required. Removing this line will give you an error. }
The name of the dialog's property is completely unrelated to the ID that you assign to your root window.
-
Hello @JKSH
Thank you for your reply therefore there still something that I don't understand.
In my main.qml I addedAboutDialog { appWindowRef: appWindow //I've changed the required property's name in AboutDialog to avoid confusion }
But I still get the same error message "Required property appWindowRef was not initialized". I feel that I'm missing something obvious, sorry about that.
And I got an other question in terms of best practices is it ok to "intialize" all the component which will need a reference to the Application Window in the main.qml file ? Or is there a cleaner way ?
Edit: There is something that I've forgot to mention.
I've also tried to initialoze a required property in an other component (just to test) which is also declared in the main.qml and in this case it is working perfectly.main.qml
ApplicationWindow { id: appWindow //... MainStackLayout { appWindowRef: appWindow anchors.top: mainToolBar.bottom anchors.bottom: mainTabBar.top currentIndex: mainTabBar.currentIndex }
MainStackLayout.qml
StackLayout { id: layout required property ApplicationWindow appWindowRef //... }
So is it supposed to be different with Dialog ?
And concerning my question about good pratices, I've look to some projects examples (those available in Qt Creator Examples tab) and it seems to be done that way. So I guess it is the right way at least for someone whith my level of knowledge which is not very high.
-