6

我使用 QtCreator 中的项目模板“Libraries/Qt Quick 2 Extension Plugin”创建了一个测试插件作为 Qt5/QML 共享库。我的开发环境是带有 Qt-5.0.0 和 QtCreator-2.6 的 linux(详情附在底部)。

源文件(关于要点):https ://gist.github.com/4467883

源文件默认生成,没有任何更改。项目名称是“untitled”,uri 是“com.mycompany.mycomponents”,对象类名称是“MyItem”。“/tmp/untitled”中的源文件作为 SRCDIR。

并构建它以将库文件输出为“libuntitled.so”“qmldir”和“/tmp/untitled-build”中的一些目标文件作为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

但是,我不能使用“/tmp/test/test.qml”中的库作为带有qmlscene命令的测试 QML 源。

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

“test.qml”(要点):https ://gist.github.com/4474422

$ qmlscene test.qml

它是失败的,并且QML_IMPORT_TRACE日志是:

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

并尝试“-I”选项:

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

那也失败了。日志是:

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

并尝试使用“/tmp/test/qmldir”:

$ vim /tmp/test/qmldir

“tmp/test/qmldir”(关于要点):https ://gist.github.com/4474497

那是失败。日志:

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

如何在 qmlscene(或 qmlviewer5)的测试 .qml("/tmp/test.qml") 中使用 library("/tmp/untitled-build/libuntitled.so")?

环境细节(基于 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

参考:

  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
4

1 回答 1

4

尝试将您plugin.so的文件和qmldir文件放在一个文件夹中,例如:

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

然后将imports目录添加到QML2_IMPORT_PATH环境变量中。 export QML2_IMPORT_PATH=/path/to/myproject/imports 这在我的 linux 机器上对我有用。

于 2013-07-02T17:13:35.357 回答