简体   繁体   中英

How to make Qt Plugin project inside a Qt App project

When I run my application, QtCreator always show an error like this

QQmlApplicationEngine failed to load component
qrc:/main.qml:3 module "SampleModules" is not installed

I tried - search for solutions everywhere - move qmldir file to application project or binary folder - change plugin - update .pro file with many ways (even add link to resource file of plugins)

My full source code is in here, I put it to github for everyone to have a whole view of my code, it just a quick sample. https://github.com/chaunnt/QtWithQMLPluginProjects

Here some details My project.pro

TEMPLATE = subdirs

    SUBDIRS += \
        SampleModules \
        SampleApp


    SampleApp.depends = SampleModules

My module.pro

TEMPLATE = lib
TARGET = SampleModules
QT += qml quick
CONFIG += plugin c++11

TARGET = $$qtLibraryTarget($$TARGET)
uri = SampleModules
version = 1.0
# Input
SOURCES +=         samplemodules_plugin.cpp         samplemodules.cpp

HEADERS +=         samplemodules_plugin.h         samplemodules.h

DISTFILES = 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_QML]/$$replace(uri, \., /)
    qmldir.path = $$installPath
    target.path = $$installPath
    INSTALLS += target qmldir
}

my app.pro

QT += quick
CONFIG += c++11

SOURCES += \
    main.cpp

RESOURCES += qml.qrc

QML_IMPORT_PATH =

QML_DESIGNER_IMPORT_PATH =

qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

I expect to make a Qt Plugin project which can register C++, QML, Singleton QML for my Qt App project.

my qmldir file

module SampleModules
plugin SampleModules
classname SampleModulesPlugin
typeinfo plugins.qmltypes

I fixed it myself. add this line to main.cpp

engine.addImportPath("..");

Root cause is because the SampleApp binary can not find the folder of QML in output directory.

It is searching for folder

"Output/SampleApp/SampleModule"

but we are outputing projects as

"Output/SampleApp"
"Output/SampleModule"

NOTE: add env variable QML_IMPORT_TRACE = 1 in Build & Run environments variables in QtCreator to get which plugins is loaded and what is wrong.

I also push the fixed patch on my github for template project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM