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. Changing width and height
Forum Updated to NodeBB v4.3 + New Features

Changing width and height

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 936 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.
  • 1 Offline
    1 Offline
    12joe31
    wrote on last edited by
    #1

    Hello,

    i just started learning QML and i have a really simple problem: i want to change the size of my main window with my cpp code and i just does not work :(

    Here is a little example:
    @
    import QtQuick 2.0

    Rectangle {
    width: 360
    height: 360
    color: "white"

    Rectangle {
        x: 200
        y: 203
        width: 96
        height: 92
        color: "yellow"
        objectName: "objectName"
    }
    

    }
    @

    @
    #include <QtGui/QGuiApplication>
    #include "qtquick2applicationviewer.h"
    #include "qqmlengine.h"
    #include "qqmlcomponent.h"
    #include "qquickitem.h"

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile&#40;QStringLiteral("qml/Test/main.qml"&#41;);
    
    QQuickItem *object = viewer.rootObject();
    QQuickItem *rect = object->findChild<QQuickItem*>("objectName");
    
    object->setProperty("width", 20);
    object->setProperty("height", 20);
    object->setProperty("color", "black");
    
    rect->setProperty("width", 10);
    rect->setProperty("height", 10);
    
    viewer.showExpanded();
    
    return app.exec();
    

    }
    @

    Those two lines just don't do anything:
    @
    object->setProperty("width", 20);
    object->setProperty("height", 20);
    @

    Changing the color of object works. Changing width and height of rect also works. The only thing that does not work is changing the size of object and i cant figure out why. Changing width and height in my QML code also works.

    Can anyone please tell me how i can change this size?? Thanks! :)

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      Hi, you can try changing the size of the QtQuick2ApplicationViewer itself instead of the root object:
      @
      viewer.setWidth(100);
      viewer.setHeight(100);
      @
      that might also change the size of the root object I think. Maybe try it yourself :)

      1 Reply Last reply
      0
      • 1 Offline
        1 Offline
        12joe31
        wrote on last edited by
        #3

        Works! Thank you :)

        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