James Feng
James Feng

Reputation: 314

How to load qt quick ui form from QtQuickWidget?

I Have a qt quick ui form, which contains two files: LogPanel.qml LogPanelForm.ui.qml

LogPanelForm.ui.qml file:

import QtQuick 2.4
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3

Item {
    id: item1
    width: 800
    height: 600
    property alias btnAdd: btnAdd

    Button {
        id: btnAdd
        text: "add"
    }
}

LogPanel.qml file:

import QtQuick 2.4
import QtQuick.Dialogs 1.2
import QtQuick.Window 2.3

LogPanelForm {
    btnAdd.onClicked: {
        console.log("1")
    }
} 

I want to load this quick ui form from C++ code:

If I set source LogPanel.qml :

QQuickWidget *view = new QQuickWidget();
view->setSource(QUrl::fromLocalFile(":/qml/LogPanel.qml"));
view->show();

The form controls will not show. And there's error message:

file::/qml/LogPanel.qml:5:1: LogPanelForm is not a type 
    LogPanelForm {
    ^ 

If I set source LogPanelForm.ui.qml :

QQuickWidget *view = new QQuickWidget();
view->setSource(QUrl::fromLocalFile(":/qml/LogPanelForm.ui.qml"));
view->show();

The onClick event in LogPanel.qml will not be called.

How should I load the form from C++ code, that both controls and event handings will work?

Upvotes: 2

Views: 967

Answers (2)

Dmitry Sazonov
Dmitry Sazonov

Reputation: 8994

You are using QUrl::fromLocalFile instead of implicit version of QUrl. Problem is in actual url. It should start with qrc:/ as you already have researched.

Also, there is a usefull API for managing resources: QDir::setSearchPaths (or QDir::addSearchPath). It may help to manage your paths. For example, you can load resources from real folder in debug, and from resources in release, but keeps all string references in code without changes (like myQml:/something).

Upvotes: 1

user8058766
user8058766

Reputation:

you have to load your .ui.qml file in your qml file:

QmlOne.qml:

       Window {
           QMLTwo{ //This your ui.qml file
           anchors.fill: parent
      }

Greetings,

Ben

Upvotes: 1

Related Questions