Changing width and height



  • 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! :)



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



  • Works! Thank you :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.