Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a Qt Quick 2 Extension Plugin on .qml with qmlscene (or qmlviewer5)

I was create a test plugin as the Qt5/QML shared library using project templete "Libraries/Qt Quick 2 Extension Plugin" in QtCreator. My developing environment is linux with Qt-5.0.0 and QtCreator-2.6(detail is append in the bottom).

The source files (on gist): https://gist.github.com/4467883

The source files are default generated without any changes. The Project name is "untitled", the uri is "com.mycompany.mycomponents" and the Object Class-name is "MyItem". The source files in "/tmp/untitled" as a SRCDIR.

And build it to output library file as "libuntitled.so", "qmldir" and some object files in "/tmp/untitled-build" as a DESTDIR.

$ ls /tmp/untitled-build
Makefile  libuntitled.so  moc_myitem.cpp 
moc_myitem.o  moc_untitled_plugin.cpp  moc_untitled_plugin.o  myitem.o
qmldir  untitled_plugin.o

But, I cannot use the library at "/tmp/test/test.qml" as the test QML source with qmlscene command.

$ mkdir /tmp/test; cd /tmp/test
$ vim test.qml

"test.qml" (on gist): https://gist.github.com/4474422

$ qmlscene test.qml

It's fail, and QML_IMPORT_TRACE log is:

QQmlImportDatabase::addImportPath: "/usr/lib64/qt5/qml"
QQmlImportDatabase::addImportPath: "/usr/bin"
QQmlImports(file:///tmp/test/test.qml)::addImplicitImport
QQmlImports(file:///tmp/test/test.qml)::addLibraryImport: "QtQuick" 2.0 as ""
QQmlImports(file:///tmp/test/test.qml)::importExtension: loaded "/usr/lib64/qt5/qml/QtQuick.2/qmldir"
QQmlImportDatabase::importPlugin: "QtQuick" from "/usr/lib64/qt5/qml/QtQuick.2/libqtquick2plugin.so"
file:///tmp/test/test.qml:2 module "com.mycompany.mycomponents" is not installed

And try "-I" option:

$ qmlscene test.qml -I /tmp/untitled-build

That's fail too. log is:

QQmlImportDatabase::addImportPath: "/usr/lib64/qt5/qml"
QQmlImportDatabase::addImportPath: "/usr/bin"
QQmlImportDatabase::addImportPath: "/tmp/untitled-build"
QQmlImports(file:///tmp/test/test.qml)::addImplicitImport
QQmlImports(file:///tmp/test/test.qml)::addLibraryImport: "QtQuick" 2.0 as ""
QQmlImports(file:///tmp/test/test.qml)::importExtension: loaded "/usr/lib64/qt5/qml/QtQuick.2/qmldir"
QQmlImportDatabase::importPlugin: "QtQuick" from "/usr/lib64/qt5/qml/QtQuick.2/libqtquick2plugin.so"
file:///tmp/test/test.qml:2 module "com.mycompany.mycomponents" is not installed

And try with "/tmp/test/qmldir":

$ vim /tmp/test/qmldir

"tmp/test/qmldir" (on gist): https://gist.github.com/4474497

That's fail. log:

QQmlImportDatabase::addImportPath: "/usr/lib64/qt5/qml"
QQmlImportDatabase::addImportPath: "/usr/bin"
QQmlImports(file:///tmp/test/test.qml)::addImplicitImport
QQmlImports(file:///tmp/test/test.qml)::importExtension: loaded "/tmp/test/qmldir"
QQmlImportDatabase::importPlugin: ".tmp.test" from "/tmp/untitled-build/libuntitled.so"
Module '.tmp.test' does not contain a module identifier directive - it cannot be protected from external registrations.
QQmlImports(file:///tmp/test/test.qml)::addLibraryImport: "QtQuick" 2.0 as ""
QQmlImports(file:///tmp/test/test.qml)::importExtension: loaded "/usr/lib64/qt5/qml/QtQuick.2/qmldir"
QQmlImportDatabase::importPlugin: "QtQuick" from "/usr/lib64/qt5/qml/QtQuick.2/libqtquick2plugin.so"
file:///tmp/test/test.qml:2 module "com.mycompany.mycomponents" is not installed

How to use the library("/tmp/untitled-build/libuntitled.so") in the test .qml("/tmp/test.qml") with qmlscene (or qmlviewer5)?

Environment detail (based on openSUSE-12.2):

$ uname -a
Linux LH-MAIN 3.4.11-2.16-desktop #1 SMP PREEMPT Wed Sep 26 17:05:00 UTC 2012 (259fc87) x86_64 x86_64 x86_64 GNU/Linux
$ g++ --version | head -n1
g++ (SUSE Linux) 4.7.1 20120723 [gcc-4_7-branch revision 189773]
$ qmake -v            
QMake version 3.0
Using Qt version 5.0.0 in /usr/lib64
$ qtcreator -version 2>&1 >/dev/null | grep "^[^ ].*" | head -n1
Qt Creator 2.6.1 based on Qt 5.0.0
$ qmlviewer5 -v    
Qml debugging is enabled. Only use this in a safe environment!
Qt QML Viewer version 5.0.0

References:

  1. http://doc-snapshot.qt-project.org/5.0/qtcore/plugins-howto.html
  2. http://doc-snapshot.qt-project.org/5.0/qtcore/qtplugin.html
  3. http://qt-project.org/doc/qt-5.0/qtqml/qtqml-modules-qmldir.html
  4. http://qt-project.org/doc/qt-5.0/qtqml/examples-quick-tutorials-extending-chapter6-plugins.html
like image 269
Usagi Ito Avatar asked Jan 07 '13 12:01

Usagi Ito


1 Answers

try to put your plugin.so and the qmldir file in a folder like:

- myproject
    - imports
        - com
            - mycompany
                - mycomponents
                    - libuntitled.so
                    - qmldir

then add the imports directory to the QML2_IMPORT_PATH environment variable. export QML2_IMPORT_PATH=/path/to/myproject/imports this worked for me on my linux box.

like image 56
net.user Avatar answered Nov 15 '22 08:11

net.user