Importing components from a plugin
-
Hi !
I made my own plugin using Creating C++ Plugins for QML, and I've been trying to import components in Qt applications usingimport "qrc:/com/planed/components"
orimport com.planed.components 1.0
, which have led to unsatisfying results:Importing a directory
Using
qrc:/com/planed/components
is probably not the proper way to go, and it results in the following error:Module namespace 'com.planed.components' does not match import URI ':.com.planed.components'
Still, I've been able to import components from sub-folders such as
qrc:/com/planed/components/qml
, so it is the most functional solution I've had so far...Importing a module
Using
import com.planed.components 1.0
only imports the C++ components registered in the plugin'sregisterTypes
method. The QML components are nowhere to be found, despite the fact that they appear in theqmldir
file located atqrc:/com/planed/components/qmldir
:module com.planed.components linktarget Planedplugin optional plugin Planedplugin classname com_planed_componentsPlugin typeinfo Planed.qmltypes prefer :/com/planed/components/ View 1.0 View.qml Window 1.0 Window.qml Application 1.0 Application.qml ...
I would expect this to work:
import QtQuick 2.15 import com.planed.components 1.0 as Planed Planed.Window { title: "Main window" }
but while it doesn't complain about
com.planed.components
not existing, it does claim thatPlaned.Window
is "not a type".So, how did I end up with an empty module anyway ? How come the components declared in the
qmldir
file, which are indeed included in the Resource File System at the expected location, are not included with the module ? -
I feel you are missing engine.addImportPath("qrc:/") in main.cpp. Can you check this ?
-
Aaand your feeling was spot on ! That was indeed the issue !
Thank you very much !