QML / Cpp plugin
-
Hello !
I've read the most of Qt5.0 documentation but i keep asking myself : Ok, there's no code in the examples nor in the tutorial, it's a x.0 version, it's cool, no problem.
But, how to create Cpp plugin for QML ?
I've made a new project using QtCreator New => Create a QtQuick2.0 Extension Plugin.
I just added a red rectangle (QSGSimpleRectNode into the "updatePaintNode").
Just to see if it works. Unfortunatelly, it's not.
My pro file:
@TEMPLATE = lib
TARGET = test_plugin
QT += qml quick
CONFIG += qt pluginTARGET = $$qtLibraryTarget($$TARGET)
uri = com.mycompany.qmlcomponentsInput
SOURCES +=
test_plugin_plugin.cpp
myitem.cppHEADERS +=
test_plugin_plugin.h
myitem.hOTHER_FILES = qmldir
!equals(PRO_FILE_PWD, $$OUT_PWD) {
copy_qmldir.target = $$OUT_PWD/qmldir
copy_qmldir.depends = $$PRO_FILE_PWD/qmldir
copy_qmldir.commands = $(COPY_FILE) "$$replace(copy_qmldir.depends, /, $$QMAKE_DIR_SEP)" "$$replace(copy_qmldir.target, /, $$QMAKE_DIR_SEP)"
QMAKE_EXTRA_TARGETS += copy_qmldir
PRE_TARGETDEPS += $$copy_qmldir.target
}qmldir.files = qmldir
unix {
installPath = $$[QT_INSTALL_IMPORTS]/$$replace(uri, \., /)
qmldir.path = $$installPath
target.path = $$installPath
INSTALLS += target qmldir
}
@qmldir :
@module com.mycompany.qmlcomponents
plugin test_plugin@
and in another project :
@# Add more folders to ship with the application, here
folder_01.source = qml
folder_01.target = qml_deploy
DEPLOYMENTFOLDERS = folder_01Additional import path used to resolve QML modules in Creator's code model
QML_IMPORT_PATH = ../qmlcomponents/test_plugin/release
The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += src/main.cpp
/My src, not related/Please do not modify the following two lines. Required for deployment.
include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
qtcAddDeployment()HEADERS += /My Includes, not related/
INCLUDEPATH += ./include
../../Include
../qmlcomponents/test_pluginLIBS += -L../qmlcomponents/test_plugin -ltest_plugin
QT += qml quick core widgets testlib
OTHER_FILES += qml/Button.qml
RESOURCES += resources.qrc@
main.qml :
@
import QtQuick 2.0
import com.mycompany.qmlcomponents 1.0;Rectangle {
id: mainWindow;
width: 1280;
height: 800;
focus: true;MyItem { anchors.fill: parent; }
}@
It's preety simple i guess. (The registerType is in another class, generated by QtCreator when you create a "QtQuick2.0 Extension Plugin" project.)
Is there something missing ?
Thanks for reading
-
Thanks for replying.
I actually already tested to add import path to my view engine :
@ m_view->engine()->addImportPath(QLatin1String("file:///C:/HMI/qmlcomponents/test_plugin/release"));
qDebug() << m_view->engine()->importPathList();@and the dump of the import path list is ok, there is the path to where the qmldir / plugins.qmltypes / dll file are.
Unfortunately, nothing changed...
-
The "no code in the docs" is a bug in the docs - code snippets aren't being generated by qdoc correctly, and I don't know why. I've asked Jerome about it on IRC and he said he'd look into it.
Regarding your module:
http://doc-snapshot.qt-project.org/qt5-stable/qtqml/qtqml-modules-identifiedmodules.html explains that you must install to QML2_IMPORT_PATH for QML2/QtQuick2 plugins (where you seem to use QT_INSTALL_IMPORTS in your project) - by default I think this is /usr/lib/qt5/qml on Linux, so your particular module would be installed to /usr/lib/qt5/qml/com/mycompany/components on Linux. You're on Windows, I see, but I don't know what the default import path for QML2 modules is on that platform.If addImportPath() isn't working as specified, that's a bug - please file a bug report and assign it to Alan Alpert. There are unit tests for that functionality, however, so either you've got a strange environment, or something strange is going on.
See http://doc-snapshot.qt-project.org/qt5-stable/qtdoc/qtquick-debugging.html#debugging-module-imports for more information about debugging imports. QML_IMPORT_TRACE=1 ./myapp should tell you a bit more about what's going on.
Cheers,
Chris. -
Ok.
Basically, what is working is copy/paste the plugin (ie. the dynamic lib, the plugins.qmltypes and the qmldir) into the Windows default qml directory... So the normal behaviour of the
(Which is fyi : C:/Qt/Qt5.0.2/5.0.2/qml)
QT_INSTALL_IMPORT seems to be used only with the "unix" tag on the project file. As i'm not compiling for Unix, the "QML_IMPORT_PATH" should add to the import path the correct place.
The addImportPath() add the correct path to the Import path but... it still prompts me that my plugin is "not installed".
How do you make a bug report ?
-
Not QML_IMPORT_PATH. QML2_IMPORT_PATH. When the version was bumped, the import path env var changed.
But you're right, the addImportPath() should also add the path.
Make a bug report on bugreports.qt-project.orgCheers,
Chris. -
Hey, thanks for your reply.
I've actually "QML_IMPORT_PATH" in my project file 'cause QML2_IMPORT_PATH doesn't seems to exists. With QML_IMPORT_PATH, qtcreator seems to see my lib (but still it prompts that it's not installed) and with QML2_IMPORT_PATH, it doesn't seems to see my lib (Warning on the qml file, import fail).
I'm writing a bug report.
-
QML_IMPORT_PATH might be what QtCreator uses to determine typenames and so forth, I don't know. But the QML engine uses QML2_IMPORT_PATH when it resolves plugins at runtime.