How to adapt Item size to a QWidget
-
Hello,
I try to put an Item (a
Rectangle
for example) in aQQuickWidget
that I will put in aQDockWidget
.
My problem is that if I resize myQDockWidget
, I don't know how to say to theRectangle
to adapt its size.
When I use QML, I can fixanchors
on parent but because the QMLrectangle is fixed on aQWidge
t, 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 myRectangle
size to 400*400. But I would like that if I change myQDockWidget
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 ! -
Hi,
AFAIK, just create your QtQuick GUI as usual and load it in the QQuickWidget.
-
@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:
InQML
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 !