How to adapt Item size to a QWidget



  • Hello,

    I try to put an Item (a Rectangle for example) in a QQuickWidget that I will put in a QDockWidget.
    My problem is that if I resize my QDockWidget, I don't know how to say to the Rectangle to adapt its size.
    When I use QML, I can fixanchors on parent but because the QMLrectangle is fixed on a QWidget, I don't know how to fix anchors on a "non" qml object.

    For example, if my QDockWidget is 400x400 sized, I can fix manually my Rectangle size to 400*400. But I would like that if I change my QDockWidget size during the execution that the Rectangle size is automatically changed.

    I can give you a code example if my explanation is not clear.
    Thanks a lot !


  • Lifetime Qt Champion

    Hi,

    AFAIK, just create your QtQuick GUI as usual and load it in the QQuickWidget.


  • Moderators

    @Fheanor As long as your rectangle fills the dock widget it should resize on it's own.

    I.e.

    Rectangle {
       anchors.fill: parent
       color: "black"
    }
    

    That should resize with the container just fine with no extra work from you. Just a guess though, didn't actually confirm it. ;)



  • @ambershark said in How to adapt Item size to a QWidget:

    @Fheanor As long as your rectangle fills the dock widget it should resize on it's own.

    I.e.

    Rectangle {
       anchors.fill: parent
       color: "black"
    }
    

    That should resize with the container just fine with no extra work from you. Just a guess though, didn't actually confirm it. ;)

    Hello, I tried this solution but it didn't work because parent is not defined.

    I finally found a solution:
    In QML I do not need to do anything:

    Rectangle {
          color: "black"
        //Nothing else to do
    }
    

    And then in my cpp file where I use my DockWidget:

        QQuickWidget *view = new QQuickWidget;
        view->setSource(QUrl::fromLocalFile("qmlFile"));
        view->setGeometry(0, 200, 600, 400);    //This is where you set the size
        view->setResizeMode(QQuickWidget::SizeRootObjectToView);  //Note that it also works with QQuickView for example !
    
        dockWidget->setWidget(view);
    

    Thanks a lot for the help and have a good day !


Log in to reply
 

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