4

How can i resize automatically QML widget?

I have QWidget created by hand. In this widget created QML component.

But when i resize QWidget, QML component doesn't resize.

Some code...

I have MyCustomQWidget class

Header:

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

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 main gui frame widget

QWidget* pCustomGUI = new MyCustomQWidget(…)
pVLayoutLeft->addWidget(pCustomGUI->getGUI);

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

4 Comments

I'm using QDeclarativeContext *ctxt (
But that does not render the QML then, does it? Where do you encounter the problem with the resizing? (Best would be some short example code added to your question.)
Hmm, I don't quite see, why you have the MyCustomQWidget, which creates a new QWidget, but that at least seems to lack some connection between m_gui and the QDeclarativeView. You should do something with layouts to handle the resize operation. As soon as the view is correctly resized, view->setResizeMode(QDeclarativeView::SizeRootObjectToView) should resize the root object of the QML context. (btw that last line has getGUI instead of getGUI() but I guess it's just a typo)
Steffen thanks for idea about "lack connection". Just remove QWidget* m_GUI and return directly view in getGUI() function. Thanks!
2

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

It is somewhat traditional to create a widget with no explicit parent and let the layout assign the parent when you add the widget.

I'm not really sure why you have 3 layers of widgets here but assuming you can't just sub-class QDeclarativeView for your custom widget, you might end up with 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);
}

Comments

0
FocusScope
{
     anchors.fill: parent


     [... some qml]
}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.