Search code examples
c++qtqquickview

QQuickView content not resizing properly


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.


Solution

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