How I can add *.qml files to my plugin?
-
Hi everyone!
I have a QML plugin 2.0 with name Msl. The most part of plugin's logic is a C++ code but my plugin must have also a QML GUI object. I tryed to find some information in doc and found article about qmldir file. I trying to create a correct qmldir file and all works fine if my plugin doesn't use QML files. I added to qmldir file a line with my qml file but client app cann't find it as a part of my plugin. My qmldir file looks like this:
@
module com.shav.msl
MslQmlControllerItem 1.0 MslQmlControllerItem.qml //This file saved in folder with qmldir file.
plugin Msl
typeinfo msl.qmltypes //Saved only C++ classes
@In client app I use this model like:
@
//others QML moduls
import com.shav.msl 1.0ApplicationWindow
{
id: appRoot
title: "Mobile Script Language Editor"
width: 1024
height: 700//This works fine because this class save in C++ code MslManager { id: manager } //This id can't be find in module MslQmlControllerItem { id: item width: 30 height: 50 }
@
Could you tell me what I do wrong? Thanks for the any help!
-
You can pack QML into the binary using QRC (Qt Resource System).
-
It's basically transparent, especially when used in QML. Once you have the QRC file created, all files within it are available to your application/ library in read-only mode. See "link":http://qt-project.org/doc/qt-5/resources.html#using-resources-in-a-library.
Sorry but I don't have a good example of this at the moment.
-
So, I've checked the link and tried to create recourse file with name "resources.qrc". I've add the prefix "/" to resources because my QML files for plugin must be saved in folder with <plugin_name>.dylib
Also I've added the code to "*.pro" file which copy qrc file to plugin folder like this:
@
sourceQmlTypeFilePath = $$PRO_FILE_PWD/msl.qmltypes
targetQmlTypeFilePath = $$OUT_PWD/$$DESTDIR/msl.qmltypes
copy_qmldir.commands += && $(COPY_FILE) "$$replace(sourceQmlTypeFilePath, /, $$QMAKE_DIR_SEP)" "$$replace(targetQmlTypeFilePath, /, $$QMAKE_DIR_SEP)"
@In qmldir file I've added the line with my QML file:
@
module com.shav.msl
MslQmlControllerItem 1.0 MslQmlControllerItem.qml
plugin Msl
typeinfo msl.qmltypes
@But I can't understand how I can load it from QML file in client App. I trying to import my plugin to QML file in client app using line:
@
import com.shav.msl 1.0
@The client QMl file can't find any QML files. I can use only C++ classes which I've added from C++ code. So, what I must add to my client app to enable it?
Thanks for the any help!
P.S. I've tried to add code import "qrc:/" to enable resources from plugin but application still can't find any others QML files.
-
[quote author="shav" date="1391604455"]
Also I've added the code to "*.pro" file which copy qrc file to plugin folder[/quote]This is unnecessary. The QML files are compiled into your library itself (they are inside the binary).
[quote author="shav" date="1391604455"]
P.S. I've tried to add code import "qrc:/" to enable resources from plugin but application still can't find any others QML files.[/quote]Hm, I was about to recommend doing that.
According to the docs, you need to run
@
Q_INIT_RESOURCE(resourceName);
@in your client application. I've never used resources in a QML plugin, so I am not sure that this thing will work. But - at least in principle - it should be doable.
-
I've added changed mu main.cpp file in client app to:
@
#include "qtquick2controlsapplicationviewer.h"int main(int argc, char *argv[])
{
Application app(argc, argv);Q_INIT_RESOURCE(recources); QtQuick2ControlsApplicationViewer viewer; viewer.setMainQmlFile(QStringLiteral("qml/MslEditor/main.qml")); viewer.show(); return app.exec();
}
@But when I compile it I receiving the error:
@
Undefined symbols for architecture x86_64:
"qInitResources_recources()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [MslEditor.app/Contents/MacOS/MslEditor] Error 1
make: *** [sub-MslEditor-MslEditor-pro-make_first] Error 2
15:14:54: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project MobileScriptLanguge (kit: Desktop Qt 5.2.0 clang 64bit)
When executing step 'Make'
@If I remove the line:
@ Q_INIT_RESOURCE(recources);
@application is build correctly. Should I added the resource file to my client app? Or I need to enable it any others way?
By the way I read the doc using "link":http://qt-project.org/doc/qt-5.0/qtqml/qtqml-modules-qmldir.html If I understand correct I may use qmldir file to add QML file to plugin but this is not work or I do something wrong. Could you tell me how you use QML files in your plugin. For example how it's work in Qt Quick Controls? As I know this plugin usage C++ code with QML files, right? If so, how I can use qmldir file correctly with QML files for plugin?
-
I don't know. My apps do not use plugins.
Maybe somebody more informed will join the discussion.
-
Hi,
I wouldn't use a QRC if you can avoid it, but that's personal preference.
Your qmldir file looks ok (although the plugin line should probably come before any qml document defined type registrations).
@
module com.shav.msl
plugin Msl
typeinfo msl.qmltypes
MslQmlControllerItem 1.0 MslQmlControllerItem.qml
@Install all of your files to $QML2_IMPORT_DIR/com/shav/msl/
In your QML files, doimport com.shav.msl 1.0
You can get more information about the imports by running your application with QML_IMPORT_TRACE=1
See the section on debugging imports from:
http://qt-project.org/doc/qt-5.0/qtquick/qtquick-debugging.htmlFor more information on importing modules, see the docs about creating identified (installed) QML modules:
http://qt-project.org/doc/qt-5.0/qtqml/qtqml-modules-topic.html
http://qt-project.org/doc/qt-5.0/qtqml/qtqml-modules-identifiedmodules.html
http://qt-project.org/doc/qt-5.0/qtqml/qtqml-modules-cppplugins.htmlCheers,
Chris. -
Hi,
Thanks for the reply. I've done all you are told but I still have a problem.
[quote author="chrisadams" date="1391648897"]
You can get more information about the imports by running your application with QML_IMPORT_TRACE=1
[/quote]I've added a new import folder to my main.cpp and now it's looks like:
@int main(int argc, char *argv[])
{
Application app(argc, argv);QtQuick2ControlsApplicationViewer viewer; viewer.addImportPath(Application::applicationDirPath()+"/com/shav/msl"); viewer.addImportPath(Application::applicationDirPath()+"/com/shav/msl/qmldir"); viewer.setMainQmlFile(QStringLiteral("qml/MslEditor/main.qml")); viewer.show(); return app.exec();
}
@When I load my app with
@
QML_IMPORT_TRACE=1
@The app outputs show a big information with qml pathes and I can check the path to the my plugin. After I've added the two path I saw this lines:
@
QQmlImportDatabase::addImportPath: "/Volumes/Documents/Applications/Qt/Qt5.2.1/5.2.1/clang_64/qml"
QQmlImportDatabase::addImportPath: "/Volumes/Documents/PROJECTS/My Projects/GIT_IOS_5/Msl/MslEditor/1.x/build-MobileScriptLanguge-Desktop_Qt_5_2_1_clang_64bit-Debug/MslEditor/MslEditor.app/Contents/MacOS"
QQmlImportDatabase::addImportPath: "/Volumes/Documents/PROJECTS/My Projects/GIT_IOS_5/Msl/MslEditor/1.x/build-MobileScriptLanguge-Desktop_Qt_5_2_1_clang_64bit-Debug/MslEditor/MslEditor.app/Contents/MacOS/com/shav/msl"
QQmlImportDatabase::addImportPath: "/Volumes/Documents/PROJECTS/My Projects/GIT_IOS_5/Msl/MslEditor/1.x/build-MobileScriptLanguge-Desktop_Qt_5_2_1_clang_64bit-Debug/MslEditor/MslEditor.app/Contents/MacOS/com/shav/msl/qmldir"
@As you can see all path is correct. But when I try to add a my qml file to main.qml QtCreator can't see it and show it as error (incorrect type). After compile client app I receive error:
@
file:///Volumes/Documents/PROJECTS/My Projects/GIT_IOS_5/Msl/MslEditor/1.x/build-MobileScriptLanguge-Desktop_Qt_5_2_1_clang_64bit-Debug/MslEditor/MslEditor.app/Contents/Resources/qml/MslEditor/main.qml:77 Type MainMenu unavailable
file:///Volumes/Documents/PROJECTS/My Projects/GIT_IOS_5/Msl/MslEditor/1.x/build-MobileScriptLanguge-Desktop_Qt_5_2_1_clang_64bit-Debug/MslEditor/MslEditor.app/Contents/Resources/qml/MslEditor/MainMenu.qml:23 Cannot assign object to listQQmlComponent: Component is not ready
Error: Your root item has to be a Window.
The program has unexpectedly finished.
@But when I trying to find some information about my QML file I see this lines:
@
QQmlImports(file:///Volumes/Documents/PROJECTS/My Projects/GIT_IOS_5/Msl/MslEditor/1.x/build-MobileScriptLanguge-Desktop_Qt_5_2_1_clang_64bit-Debug/MslEditor/MslEditor.app/Contents/Resources/qml/MslEditor/MainMenu.qml)::resolveType: "MslQmlControllerItem" => "" QUrl("file:///Volumes/Documents/PROJECTS/My Projects/GIT_IOS_5/Msl/MslEditor/1.x/build-MobileScriptLanguge-Desktop_Qt_5_2_1_clang_64bit-Debug/MslEditor/MslEditor.app/Contents/MacOS/com/shav/msl/MslQmlControllerItem.qml") TYPE/URL
@As you can see my QML file is detected correct.
Also I've check links which you added to your post.
Could you tell me what I do wrong?Thanks for the help!
-
What is at line 23 of your MainMenu.qml?
From the errors, it seems like there's an unrelated error in your MainMenu.qml file, which is causing it to fail.
I think it is resolving the MslQmlControllerItem type properly, now - at least, I don't see any errors about an unresolved typename.Cheers,
Chris. -
Hi,
Thanks for the reply! I've checked my MainMenu.qml and you are right I can't add my QML file to Menu item. I've removed it from MainMenu.qml and add to main.qml (ApplicationWindow object). For now application is compiled and load but than it's crashes without any message only this:
@
Starting /Volumes/Documents/MyWork/PROJECTS/QtProjects/GIT/Msl/Desktop/build-MobileScriptLanguge-Desktop_Qt_5_2_1_clang_64bit-Debug/MslEditor/MslEditor.app/Contents/MacOS/MslEditor...
QML debugging is enabled. Only use this in a safe environment.
The program has unexpectedly finished.
/Volumes/Documents/MyWork/PROJECTS/QtProjects/GIT/Msl/Desktop/build-MobileScriptLanguge-Desktop_Qt_5_2_1_clang_64bit-Debug/MslEditor/MslEditor.app/Contents/MacOS/MslEditor crashed
@If I remove my new object it's works fine. I can't understand why application can't use my QML files.
This is how looks like my MslQmlControllerItem.qml file:
@
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Dialogs 1.1
import QtQuick.Window 2.0
import QtQuick.Layouts 1.1
import com.shav.msl 1.0Rectangle {
id: controllerRoot
color: "transparent"
clip: trueproperty MslController controller: null property var appRoot: null MslControllerItem { id: controllerView clip: true width: 320 height: 480 controller: controllerRoot.controller }
}
@I've get this crash stack trace:
@
Date/Time: 2014-02-07 09:46:58.731 +0200
OS Version: Mac OS X 10.9.1 (13B42)
Report Version: 11
Anonymous UUID: C9A6A262-1339-681C-6013-34CB4E59B2DFCrashed Thread: 6 QSGRenderThread
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000000000000d8Thread 6 Crashed:: QSGRenderThread
0 QtQuick 0x000000010007a1f4 QSGGeometry::updateRectGeometry(QSGGeometry*, QRectF const&) + 4
1 QtQuick 0x0000000100086de5 QSGSimpleRectNode::setRect(QRectF const&) + 21
2 libMsl_debug.dylib 0x000000010c1af24e MslControllerItem::updatePaintNode(QSGNode*, QQuickItem::UpdatePaintNodeData*) + 254 (mslcontrolleritem.cpp:95)
3 QtQuick 0x00000001000d21f5 QQuickWindowPrivate::updateDirtyNode(QQuickItem*) + 3317
4 QtQuick 0x00000001000c9c99 QQuickWindowPrivate::syncSceneGraph() + 313
5 QtQuick 0x00000001000a7837 QSGRenderThread::sync() + 103
6 QtQuick 0x00000001000a7922 QSGRenderThread::syncAndRender() + 114
7 QtQuick 0x00000001000a7d58 QSGRenderThread::run() + 168
8 QtCore 0x00000001009d1392 QThreadPrivate::start(void*) + 338
9 libsystem_pthread.dylib 0x00007fff90663899 _pthread_body + 138
10 libsystem_pthread.dylib 0x00007fff9066372a _pthread_start + 137
11 libsystem_pthread.dylib 0x00007fff90667fc9 thread_start + 13Thread 6 crashed with X86 Thread State (64-bit):
rax: 0x00000001108c4c10 rbx: 0x0000000000000000 rcx: 0x000000010c23b5d0 rdx: 0x00000001076afd10
rdi: 0x00000000000000b8 rsi: 0x00000001108c4c10 rbp: 0x00000001108c4ba0 rsp: 0x00000001108c4ba0
r8: 0x0000000000000004 r9: 0x0000000101e00000 r10: 0x00000001002fc1a0 r11: 0x0000000100086dd0
r12: 0x0000000000003850 r13: 0x0000000000000000 r14: 0x00000001076afde0 r15: 0x00000001108c4ca8
rip: 0x000000010007a1f4 rfl: 0x0000000000010202 cr2: 0x00000000000000d8
@Thanks for the help!
-
Hi,
Small update about my problem. I've found a bug. But now, I can't understand how this fix. In my QML file from plugin I've remove line:
@
import com.shav.msl 1.0
@This helped to me compile the app and load it. But now I have a question: How I can use classes from my C++ plugin in this QML file? If I remove this line my QML file can't use C++ classes from plugin.
Thanks for the help.