Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Window width and height in QML
Forum Updated to NodeBB v4.3 + New Features

Window width and height in QML

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 4 Posters 8.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maksim1979
    wrote on last edited by
    #1

    Hello

    I've got a strange question. Is it possible to find out a root qml Item size when the window is maximized (viewer.showMaximized(); method is used). I'm asking because this code

    @
    Item {
    id: root
    Component.onCompleted: console.log("width" + root.width)
    }
    @

    Prints 0 and as a result I cannot adjust item's children base on parent width/height.
    I've tried to play with viewer.setResizeMode(QQuickView::SizeRootObjectToView); but it's not going to help.

    Thanks a lot,
    Maxim

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Duff
      wrote on last edited by
      #2

      Hi,

      Not sure if this helps but I have found that if I import QtQuick.Window 2.1 then I can use Screen.desktopAvailableWidth and Screen.desktopAvailableHeight. Hope that helps

      --Duff

      1 Reply Last reply
      0
      • O Offline
        O Offline
        onek24
        wrote on last edited by
        #3

        Hey,

        first of all: root.width is printing you 0 because it's width is 0. From where do you want to find out your QML Item size? If you want to find it out from QML then you'll have to do it like you already did. If you want to find it out from Cpp use QObject::property on your rootObject. Could you please describe more what you're trying to do?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maksim1979
          wrote on last edited by
          #4

          bq. "Could you please describe more what you’re trying to do?"

          Ok, first of all I maximize my window from C++ code: viewer.showMaximized(); Since the window is maximized I cannot specify exact width&height in qml file because I don't know them. Later, in "Component.onCompleted" I'm going to create dynamically a dialog (a Rectangle) and put it into "root" item center. So It should look something like that:

          @
          var dialog = dialogComponent.createObject(root);
          dialog.x = (root.width - dialog.width) / 2;
          dialog.y = (root.height - dialog.height) / 2;
          @

          But since root.width/height is not set explicitly it's not going to work.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Lucijan
            wrote on last edited by
            #5

            maksim1979, it works fine for me:

            @import QtQuick 2.0

            Rectangle {
            id: root;
            width: 256;
            height: 306;

            MouseArea {
                anchors.fill: parent
                onClicked: {console.log(root.width + " " + root.height)}
            }
            

            }@

            So, even if width and height are set, showMaximized() will override that here and the correct width and height will be printed to the console.
            Sorry if I'm misunderstanding what you're trying to say.

            EDIT: I also don't think you'll need to create the dialog dynamically. If you don't need to move the dialog around, you can just set its anchors.centerIn: parent. If you do need to move it, then its x and y properties like this:

            @x: (root.width - dialog.width)/2
            y: (root.height - dialog.height)/2@

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved