6

I am a beginner in QML and try to insert a QML View in QWdiget but I don't understand why it doesn't work.

Here is a simple example of my qml file (this is not the real file):

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.2
import QtQml.Models 2.1

ObjectModel {
  id: itemModel
  Rectangle {
    color: "orange"
    anchors.fill: parent
  }
  Rectangle {
    color: "orange"
    anchors.fill: parent
  }
  Rectangle {
    color: "orange"
    anchors.fill: parent
  }
  
  ListView {
    id: my_list
    anchors.fill: parent
    model: itemModel
  }
}

And this is how I load it in my mainwindow:

QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setMinimumSize(200, 200);
container->setFocusPolicy(Qt::TabFocus);
view->setSource(QUrl("main.qml"));
ui->dockWidget->setWidget(container);

How could I insert my view in a QWidget? At this time, I really need to use a QML view and because I need to use it in an already existing application, I can't just use a QML project.

Thanks a lot for your help and have a good day!

2 Answers 2

10

There exist a special QQuickWidget, dedicated to that exact purpose.

QQuickWidget *view = new QQuickWidget;
view->setSource(QUrl::fromLocalFile("myqmlfile.qml"));
view->show();
4
  • Hello ! Thanks for your answer. However, even when I try to use QQuickWidget, I have the following error: Syntax error : ListView { And I don't understand what is wrong here.
    – Ed Nio
    Apr 6, 2017 at 6:09
  • 2
    Your code doesn't have a root item. Also, since QQuickWidget is a wrapper for a QQuickWindow, I assume the root item must be a window, either an ApplicationWindow, or simply a Window.
    – dtech
    Apr 6, 2017 at 6:34
  • This is weird because I already tried that. But if I use as root item a window, I have the following error message: QQuickWidget only supports loading of root objects that derive from QQuickItem. If your example is using QML 2, (such as qmlscene) and the .qml file you loaded has 'import QtQuick 1.0' or 'import Qt 4.7', this error will occur. To load files with 'import QtQuick 1.0' or 'import Qt 4.7', use the QDeclarativeView class in the Qt Quick 1 module. But I am not using QtQuick 1.0 or Qt 4.7. And if I use a DeclarativeView, it won't support import QtQuick 2.4
    – Ed Nio
    Apr 6, 2017 at 6:54
  • So does it work if your root is say... a rectangle? It may actually be that it is already already a Window so it doesn't need or support the root to be a window. Looks like it is "QQmlApplicationEngine" that requires the root to be a window unlike the qtquick view, which don't support window root.
    – dtech
    Apr 6, 2017 at 9:13
3
  1. QQmlApplicationEngine *m_engine in MainWindow.h
  2. in MainWindows.cpp set :
m_engine->addImportPath("qrc:/qml/imports");
m_engine->load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
 // m_engine->rootContext()->setContextProperty("mainWindows", this);
qDebug() << "Ok engine created";`
`QWindow *qmlWindow = qobject_cast<QWindow*>(m_engine->rootObjects().at(0));
QWidget *container = QWidget::createWindowContainer(qmlWindow, this);
container->setMinimumSize(200, 200);
container->setMaximumSize(1200, 900);
ui->verticalLayout->addWidget(container);`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.