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. How to adapt Item size to a QWidget
Qt 6.11 is out! See what's new in the release blog

How to adapt Item size to a QWidget

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 1.9k Views 2 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.
  • F Offline
    F Offline
    Fheanor
    wrote on last edited by
    #1

    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 !

    A 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • F Fheanor

        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 !

        A Offline
        A Offline
        ambershark
        wrote on last edited by
        #3

        @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. ;)

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Fheanor
          wrote on last edited by Fheanor
          #4

          @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 !

          1 Reply Last reply
          1

          • Login

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