How to resize a QML widget embedded in a QWidget?

How can I automatically resize a QML widget?

I have a manually created QWidget. A QML component has been created in this widget.

But when I resize the QWidget, the QML component does not change.

Some codes ...

I have a class MyCustomQWidget

Title:

Class MyCustomQWidget : public QWidget { Q_OBJECT public: QDeclarativeView* view; private: QWidget* m_GUI; public: QWidget* getGUI() {return m_GUI;}; } 

A source:

 MyCustomQWidget:: MyCustomQWidget (QWidget *parent) :QWidget(parent) { m_GUI = new QWidget(); view = new QDeclarativeView(m_GUI); view->setSource(QUrl("qrc:/qml/gui.qml")); //view->setResizeMode(QDeclarativeView::SizeRootObjectToView); } 

In the main gui frame widgets

 QWidget* pCustomGUI = new MyCustomQWidget(…) pVLayoutLeft->addWidget(pCustomGUI->getGUI); 
+4
source share
3 answers

There is not much detail in the question, but if you are using QDeclarativeView to display QML, look at its setResizeMode() member. Setting this parameter to QDeclarativeView::SizeRootObjectToView can just do what you are looking for: it automatically resizes the QML root object to the size of the view.

+6
source

When you place a Qt widget inside another Qt widget, you must manually resize it or use the layout to do this automatically.

It is somewhat traditional to create a widget without an explicit parent and allow the layout to assign a parent when adding a widget.

I'm not sure why you have 3 widget layers, but if you can't just subclass QDeclarativeView for your custom widget, you can get something like this:

 Class MyCustomQWidget : public QWidget { Q_OBJECT private: QDeclarativeView* view; } 

and

 MyCustomQWidget:: MyCustomQWidget (QWidget *parent) : QWidget(parent) { QHBoxLayout *box = new QHBoxLayout(this); view = new QDeclarativeView; view->setSource(QUrl("qrc:/qml/gui.qml")); //view->setResizeMode(QDeclarativeView::SizeRootObjectToView); box->addWidget(view); } 
+2
source
 FocusScope { anchors.fill: parent [... some qml] } 

This corresponds to the FocusScope size of the parent object, in this case QDeclarativeView.

0
source

Source: https://habr.com/ru/post/1343614/


All Articles