HammerSpb
HammerSpb

Reputation: 735

How to resize QML widget embedded in QWidget?

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

Upvotes: 4

Views: 5910

Answers (3)

Lincoln Ramsay
Lincoln Ramsay

Reputation: 21

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);
}

Upvotes: 2

mmoment
mmoment

Reputation: 1289

FocusScope
{
     anchors.fill: parent


     [... some qml]
}

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

Upvotes: 0

Steffen
Steffen

Reputation: 2948

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.

Upvotes: 6

Related Questions