3

I am trying to embed a QMLview inside a QWidget. I managed to display the view inside the mainwindow without any problem, but whenever i am resizing it, the qml view is not updated.

View not resizing properly

Here is the code which draws that view

mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);

  QQuickView * view = new QQuickView ();
  view->setSource (QUrl ("qrc:///main//qml-map") );
  view->setResizeMode (QQuickView::SizeRootObjectToView);

  QWidget * container = QWidget::createWindowContainer(view, this);

  setCentralWidget(container);
}

main.qml

Item {
  anchors.fill: parent
  visible: true

  Plugin {
    id: osmPlugin
    name: "osm"
  }

  Map {
    visible: true
    anchors.fill: parent
    plugin: osmPlugin
    center: QtPositioning.coordinate(59.91, 10.75) // Oslo
    zoomLevel: 10
  }
}

I also tried to put the container using the setLayout function, but it didn't do the trick.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
kabinja
  • 135
  • 2
  • 10

1 Answers1

3

i faced the same problem. The only way i found to fix it is to handle resizeEvent of host widget and reset width and height properties of root qml component according to new size. Try this:

void MainWindow::resizeEvent(QResizeEvent* event)
{
  QQuickItem* rootObject =  view->rootObject(); 
  QSize newSize = event->size();
  if(rootObject) rootObject->setProperty("width",QVariant::fromValue(newSize.width()));
  if(rootObject) rootObject->setProperty("height",QVariant::fromValue(newSize.height()));
}
  • 1
    I would also recommend to add `QMainWindow::resizeEvent(event);` at the end to ensure that the resize event is properly propagated down the line. I do find it strange that QQuickWidget etc. don't do this properly out of the box. I'm rather confused what the `setResizeMode(...)` is supposed to do since the two possible values there don't seem to do what they are described to. Or maybe the documentation is just confusing and we are all missing the point here. – rbaleksandar Jun 14 '20 at 08:57