[solved] Change app width from QML code



  • Hello,

    I need to resize my application from QML code. But I encountered that I can change only height, but not the width, although the code is the same. Here is test app that demonstrates the issue.

    Tested on Qt 4.8.0/4.8.1, Windows XP/7.

    main.cpp:
    @#include <QApplication>
    #include <QtDeclarative/QDeclarativeContext>
    #include "qmlapplicationviewer.h"

    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
    QScopedPointer<QApplication> app(createApplication(argc, argv));
    QmlApplicationViewer viewer;
    viewer.setMainQmlFile(QLatin1String("qml/untitled1/main.qml"));
    viewer.rootContext()->setContextProperty("mainwindow", &viewer);
    viewer.showExpanded();
    return app->exec();
    }
    @

    main.qml:
    @
    import QtQuick 1.1

    Rectangle {
    width: 400; height: 400

    Rectangle {
        width: 100; height: 80; color: "yellow"
        Text { text: qsTr("Change height"&#41;; anchors.centerIn: parent }
        MouseArea {
            anchors.fill: parent
            onClicked: { mainwindow.size.height -= 20 } // OK
        }
    }
    
    Rectangle {
        x: 100; width: 100; height: 80; color: "lightgreen"
        Text { text: qsTr("Change width"&#41;; anchors.centerIn: parent }
        MouseArea {
            anchors.fill: parent
            onClicked: { mainwindow.size.width -= 20 } // NO EFFECT
        }
    }
    

    }
    @



  • Found that X coordinate also cannot be changed from QML side, as well as width.

    Workaround:
    Declare signal changeXWidth(int x, int w) in qml file, connect it to c++ slot and do resize logic there.


Log in to reply
 

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