I have an application that displays a QML map inside a QWidget. The Qwidget forms part of a Horizontal layout with splitter in order to allow the map to take up most of the space. My UI structure look like the image below.
The wdgtMap is the one that is used as a window container. If I drop a text edit in there (in designer), the text edit fills the widget as expected and expands as application is resized and maximised.
If now in code, I create my window container for the map, the map does not expand to the size of the wdgtMap. I have tried many different options to no avail.
qmlMapObject = qvMap->rootObject();
// Set the context and include properties.
QQmlContext *ctxt = qvMap->rootContext();
ctxt->setContextProperty("asset_class", &clAsset);
// Set the source after defining the context.
qvMap->setSource(QUrl("qrc:/main.qml"));
// Set widget properties.
QWidget *qvMapContainer = QWidget::createWindowContainer(qvMap, ui->wdgtMap);
qvMapContainer->setMinimumSize(20,20);
qvMapContainer->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding);
In the header file under private section the following:
QQuickView *qvMap = new QQuickView();
QObject *qmlMapObject;
The second image is what I end up with.You can see the small map in the top right block. I want it to fill all that space and expand/shrink as the app is resized.
Pointers in the right direction will be much appreciated.
With the following instruction:
QWidget *qvMapContainer = QWidget::createWindowContainer(qvMap, ui->wdgtMap);
you are only creating a widget called qvMapContainer
with the content of qvMap
and as a parent ui->wdgtMap
, a widget is the children of another one only establishes that the position of this widget is relative to the parent, therefore the map was established in the top left and that the initial position of every widget is (0, 0).
If you want a widget to occupy the size of the parent, you must do it through a layout.
qmlMapObject = qvMap->rootObject();
// Set the context and include properties.
QQmlContext *ctxt = qvMap->rootContext();
ctxt->setContextProperty("asset_class", &clAsset);
// Set the source after defining the context.
qvMap->setSource(QUrl("qrc:/main.qml"));
// Set widget properties.
QWidget *qvMapContainer = QWidget::createWindowContainer(qvMap);
QVBoxLayout *lay = new QVBoxLayout(ui->wdgtMap);
lay->addWidget(qvMapContainer);