Get screen size from QML
-
Hello,
how do i set the width / height equal to the screen size in qml?
Cheers
Tom -
Hello,
I am running the QDeclarativeView in Full Screen mode.
But I am trying to set a child widget relative to the full screen Rectangle:
Rectangle {
Myrect1 { } Myrect2 { x: parent.width / 2 }
}
Myrect 2 isn't shown at the right place of the screen.
Cheers
Tom -
Tom,
to make relative width/height to the parent, you can use the code like this
@Rectangle{
// here you need to set the parent width / height properties to use relatively
width: 500
height: 500Myrect1{ } Myrect2{ // set width / height relative to the parent width: parent.width height: parent.height }
}
@ -
Hello,
I know I can do that. But I don't want to set a hard coded number because then I am stuck to one screen size. I want to retrieve the screen size of the device and use that. Would this be possible in plain QML, or do I need to do it in C++ and pass the values to QML?
Cheers
Tom -
Yes, you need to pass the values to QML from c++ look the following:
Obtaining a Desktop Widget
The QApplication::desktop() function is used to get an instance of QDesktopWidget.
The widget's screenGeometry() function provides information about the geometry of the available screens with. The number of screens available is returned by screenCount, and the screenCountChanged() signal is emitted when screens are added or removed. The screen number that a particular point or widget is located in is returned by screenNumber().
-
Hi Tom,
Have you tried setting the SizeRootObjectToView resize mode on your QDeclarativeView? That should ensure that the root object is the same size as the view. With that in place, the easiest way to center a child in its parent is with anchors:
@MyRect { anchors.centerIn: parent }@
you should also be able to center manually with e.g.
@MyRect { x: parent.width/2 - width/2; ... }@
Regards,
Michael -
Hi,
The SizeRootObjectToView works perfectly. Just what I needed.
Cheers
Tom -
[quote author="AlterX" date="1395760356"]Qt 5.x has QML Screen object you can use within an Item.[/quote]
thanks !!
i just added:
@import QtQuick.Window 2.0@
then set the width/height like :
@ApplicationWindow {
id: applicationWindow1width: Screen.width height: Screen.height@
and it works
-
[quote author="AlterX" date="1395760356"]Qt 5.x has QML Screen object you can use within an Item.[/quote]
thanks !!
i just added:
@import QtQuick.Window 2.0@
then set the width/height like :
@ApplicationWindow {
id: applicationWindow1width: Screen.width height: Screen.height@
and it works