Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get screen size from QML



  • Hello,

    how do i set the width / height equal to the screen size in qml?

    Cheers
    Tom



  • run qmlviewer in fullscreen mode



  • 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: 500

    Myrect1{ }
    
    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



  • Is there a pure QML way to achieve this?

    Okay strike that.
    -Is there a pure QML way to achieve this?-

    I found the place where to put the "--fulscreen" parameter in Qt-Creator.



  • Qt 5.x has QML Screen object you can use within an Item.



  • [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: applicationWindow1

    width: 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: applicationWindow1

    width: Screen.width
    height: Screen.height@
    

    and it works



  • Good...so tag thread's title with [SOLVED]



  • Good...so tag thread's title with [SOLVED]


Log in to reply