1

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.

enter image description here

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Mikkie
  • 103
  • 2
  • 11

1 Answers1

3

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);
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • OK, so this did not work out of the box for me. The reason is that the wdgtMap already had a layout assigned in the UI Designer form. See image in inital post. You can see that wdgtMap has a layout. So after going back to the UI and deleting that layout, the solution worked, thanks very much for the help. – Mikkie Apr 07 '18 at 08:44
  • @Mikkie I just see it, it's very suspicious, I recommend that for other questions provide the .ui, so I would have observed. :D – eyllanesc Apr 07 '18 at 08:46