QML ApplicationWindow width and height for use in child item
-
wrote on 10 Sept 2021, 19:10 last edited by
I have an item that needs to be the same width and height as the ApplicationWindow. Docs indicate that the ApplicationWindow, and therefore its width and height, should be available to all children objects via its id, which is set. But it is not.
There are static functions in Qt C++ that make this possible, but I can find no such way to get this information in QML.
This is from the QML 5.15 docs for ApplicationWindow:
"Attached ApplicationWindow Properties
Due to how Scope and Naming Resolution works in QML, it is possible to reference the id of the application root element anywhere in its child QML objects."Yes, this is the beginning of the section for attached properties, but it clearly states that ApplicationWindow should be accessible to all child objects.
Any help would be greatly appreciated. No, I do not believe going to C++ is an acceptable approach.
Thanks.
-
I have an item that needs to be the same width and height as the ApplicationWindow. Docs indicate that the ApplicationWindow, and therefore its width and height, should be available to all children objects via its id, which is set. But it is not.
There are static functions in Qt C++ that make this possible, but I can find no such way to get this information in QML.
This is from the QML 5.15 docs for ApplicationWindow:
"Attached ApplicationWindow Properties
Due to how Scope and Naming Resolution works in QML, it is possible to reference the id of the application root element anywhere in its child QML objects."Yes, this is the beginning of the section for attached properties, but it clearly states that ApplicationWindow should be accessible to all child objects.
Any help would be greatly appreciated. No, I do not believe going to C++ is an acceptable approach.
Thanks.
wrote on 10 Sept 2021, 19:15 last edited by eyllanesc 9 Oct 2021, 19:16@VFCraig All properties are accessible using the "id" if in your scope you have access to is "id", otherwise you will have to expose some (without code it is difficult to help you). But if you want to access the window associated with an item then use the
Window.window
property:YourItem{ width: Window.window.width height: Window.window.height }
-
wrote on 10 Sept 2021, 20:55 last edited by
Is that a required statement from you guys? 'Without code it is difficult to help you"? Are you that unfamiliar with the environment you are supporting?
Simple question. Looking for what in C++ would be a static method to access the geometry of the main application screen. In my case, there is only one screen. I am several levels removed from the ApplicationWindow, so I do not have direct access to it. The documentation states that using its ID any child object should be able to access it. Now you are saying that is not the case.
Of course all properties are accessible from the id. If I had access to the ID in the class I am working with, I wouldn't be asking this question. But I should NOT need it. Is there or is there not a way to get the application screen dimensions WITHOUT having direct access to the ApplicationWindow id. In other words, these are values relative to the environment configuration within which the application is running. I don't want, nor should I need, the application id to get to that information. And I don't want to go, parent.parent.parent.parent.parent.width, to get it. Because that is sure to break sometime down the road.
You don't need a code sample from me for this. You just need to tell me if it is possible in QML to get that information WITHOUT the applicationwindow id. Or is the only way to go into C++ and push the functionality up to QML.
-
Is that a required statement from you guys? 'Without code it is difficult to help you"? Are you that unfamiliar with the environment you are supporting?
Simple question. Looking for what in C++ would be a static method to access the geometry of the main application screen. In my case, there is only one screen. I am several levels removed from the ApplicationWindow, so I do not have direct access to it. The documentation states that using its ID any child object should be able to access it. Now you are saying that is not the case.
Of course all properties are accessible from the id. If I had access to the ID in the class I am working with, I wouldn't be asking this question. But I should NOT need it. Is there or is there not a way to get the application screen dimensions WITHOUT having direct access to the ApplicationWindow id. In other words, these are values relative to the environment configuration within which the application is running. I don't want, nor should I need, the application id to get to that information. And I don't want to go, parent.parent.parent.parent.parent.width, to get it. Because that is sure to break sometime down the road.
You don't need a code sample from me for this. You just need to tell me if it is possible in QML to get that information WITHOUT the applicationwindow id. Or is the only way to go into C++ and push the functionality up to QML.
wrote on 10 Sept 2021, 21:07 last edited by eyllanesc 9 Oct 2021, 21:10-
I can ask for the requirements that I consider necessary to be able to help you in the same way that you can provide it or not. Later it will depend if I continue helping you since that is the logic of cooperation in any forum. Here nobody forces anything.
-
You have an XY problem, if your goal is to get the screen size that is different from the window size then you should use
Screen
. If instead you want to get the size of the window where your item is hosted then you should useWindow.window
. Both are global variables which is similar to static variable
-
-
Is that a required statement from you guys? 'Without code it is difficult to help you"? Are you that unfamiliar with the environment you are supporting?
Simple question. Looking for what in C++ would be a static method to access the geometry of the main application screen. In my case, there is only one screen. I am several levels removed from the ApplicationWindow, so I do not have direct access to it. The documentation states that using its ID any child object should be able to access it. Now you are saying that is not the case.
Of course all properties are accessible from the id. If I had access to the ID in the class I am working with, I wouldn't be asking this question. But I should NOT need it. Is there or is there not a way to get the application screen dimensions WITHOUT having direct access to the ApplicationWindow id. In other words, these are values relative to the environment configuration within which the application is running. I don't want, nor should I need, the application id to get to that information. And I don't want to go, parent.parent.parent.parent.parent.width, to get it. Because that is sure to break sometime down the road.
You don't need a code sample from me for this. You just need to tell me if it is possible in QML to get that information WITHOUT the applicationwindow id. Or is the only way to go into C++ and push the functionality up to QML.
@VFCraig this forum is for Qt users to communicate. If you want to be entitled, contact the commercial support.
We are providing free support because that's how we feel, so yes the minimum courtesy when asking for free help is to provide a minimal reproducible example to help us help you. Especially when you are claiming a documented feature doesn't work.Referencing the id of the window in a sub context (even multiple files down) does work, at least in Qt 5 but accessing context properties like that is not recommended. @eyllanesc gave you the proper way to do it, yet you didn't thank him
-
wrote on 13 Sept 2021, 13:56 last edited by
Of course it works, but it is inappropriate and a horrendous example to provide in any programming forum. There are methods in the C++ side that do provide, via static functions, the information I request. Or at least direct access to the QApplicaion of the program, from which it would be available. THAT would be the proper way to make such available in QML (and as it is already available in C++, it is strange that it is not).
And your Window.window example does not work as it requires the item I am attempting to size BEFORE showing to be shown first before it returns anything other than ZERO for width and height.
I do NOT have an XY problem. My target screen is the same size as the main window. It is a medical device, with a FIXED screen size. I am attempting, as any programmer should, to cover for the fact that no hardware remains available indefinitely and that the PHYSICAL screen size is likely to change. What I am attempting to do is to prevent future updates from having to search the code for hard coded number.
There is no code sample that would be relevant. What I want I have clearly described.
How about,
myItem.width = theMainAppWindow.size().width()
myItem.height = theMainAppWindow.size().height()Keeping in mind that I DO NOT have access to theMainAppWindow in myItem and have absolutely NO intention of writing some parent.parent.parent.parent garbage that will break as soon as an intermediary object is added through some other class attempting to inherit it.
-
@VFCraig said in QML ApplicationWindow width and height for use in child item:
myItem.width = theMainAppWindow.size().width()
myItem.height = theMainAppWindow.size().height()This works (but it isn't recommended) if you write actual QML:
myItem.width = theMainAppWindow.width
if you are in a child context of where theMainAppWindow was declared.Of course it works, but it is inappropriate and a horrendous example to provide in any programming forum.
What are you talking about? What is inappropriate and a horrendous example?
Implicitely accessing an id from a descendant context? I agree but that's what you claimed for in your original question : "Docs indicate that the ApplicationWindow, and therefore its width and height, should be available to all children objects via its id, which is set. But it is not."We gave you an alternative :
width: Window.width
You then claim it is inapplicable to you: "And your Window.window example does not work as it requires the item I am attempting to size BEFORE showing to be shown first before it returns anything other than ZERO for width and height."Can you tell us why? Sizing it when you show it is not enough? If it's not you can still get the the Window attached object from another Item already attached to the window.
have absolutely NO intention of writing some parent.parent.parent.parent garbage
It seems that you imply that this would work although it is in fact garbage and not maintenable. If this technically works, why won't the superior
Window.width
work in your case?If for some reason you can't use the
Window
attached object, you could useQt.application.screens
. -
wrote on 14 Sept 2021, 08:39 last edited by Matthew0x
Hello,
I won't participate in the quarrel, as it's meaningless to me, however I had a similar issue to yours.
https://forum.qt.io/topic/130198/dynamic-size-qml-page-unable-to-renderI wasn't able to solve it, however I noticed one issue.
When calling properties of objects (custom defined) from a child object (when the property is of the parent)...when directly nested, you can use parent.width (some property), or (parent name).width, or
"grand grand" parent's name.width (when that object is deeper into the hierarchy, but still in the same QML file's scope I believe).My point is. I tried calling ApplicationWindow's width and height from a non-child (actually it was kind of a child, it was called from the AppWindow class so...), this is: a different QML file.
The methods such as:
- ID
- custom property in parent (for global calling the custom property's name)
- aliases
failed.
However, your idea works for me:
(it's mixed with custom properties, because I was playing around)Page { id: localPage2 width: Screen.width / 3 height: Screen.height / 3 property int boxHeight: localPage2.height / 6 property int boxWidth: localPage2.width / 3 Rectangle { id: rectangleInput x: (localPage2.width * 1 / 2) - (width * 1 / 2) y: localPage2.height * 1 / 6 width: localPage2.boxWidth height: localPage2.boxHeight radius: 5
As you can see I use the screen object's properties for assigning custom size and reading it from my rectangle for example. This way I can dynamically scale the objects inside my page.
I believe that this is what you wanted to have? I haven't tried calling AppWindow's properties directly in main.qml yet. I tried the call outside of its QML file and it failed, so perhaps there is an issue with that AppWindow object, as I had no issues accessing custom properties of objects from QML files that aren't Main.qml.
-
wrote on 14 Sept 2021, 18:51 last edited by
Matthew, thanks for that response. Think I had tried that before, but as I am developing on Windows, not directly on the target, Screen returns me 1920x1080.
But it did put me on the track to identify the OS using QML and set width and height with a trinary based on that. So if it's Windows, I hard code and if the target I can use the values from Screen. (Qt.platform.os is a read only string available as part of Qt QML...which is surprisingly hard to google for.)
1/9