Hi everyone,
I'm developing an application using QML frontend with C++ backend. I have generated a plugin which was generated from C++ and I would now like to use this in Qml.
I believe I have imported the plugin correctly since when I run the program everything seems ok. So to clarify:
I Have a class called Box. I then have a class derived from QDeclarativeExtensionPlugin called BoxPlugin to expose the Box class to qml.
void BoxPlugin::registerTypes(const char *uri)
{
qmlRegisterType<Box>(uri, 1, 0, "Box");
}
void BoxPlugin::registerTypes(const char *uri)
{
qmlRegisterType<Box>(uri, 1, 0, "Box");
}
To copy to clipboard, switch view to plain text mode
I then build the project with the following .pro file:
TEMPLATE = lib
CONFIG += qt plugin
QT += declarative
DESTDIR = plugin
TARGET = boxPlugin
... headers and source files
TEMPLATE = lib
CONFIG += qt plugin
QT += declarative
DESTDIR = plugin
TARGET = boxPlugin
... headers and source files
To copy to clipboard, switch view to plain text mode
Then in main.c I import the plugin:
QPluginLoader loader
("C:/Qt/myExamples/CppQmlProject/Src/Components/BasicComponents/plugin/boxPlugin.dll");
QDeclarativeExtensionPlugin *plugin = qobject_cast<QDeclarativeExtensionPlugin *>(loader.instance());
if (plugin)
plugin->registerTypes("BasicComponents.Boxes");
else
qDebug() << "FAIL!";
view
->setSource
(QUrl("MainWindow.qml"));
QPluginLoader loader("C:/Qt/myExamples/CppQmlProject/Src/Components/BasicComponents/plugin/boxPlugin.dll");
QDeclarativeExtensionPlugin *plugin = qobject_cast<QDeclarativeExtensionPlugin *>(loader.instance());
if (plugin)
plugin->registerTypes("BasicComponents.Boxes");
else
qDebug() << "FAIL!";
view->setSource(QUrl("MainWindow.qml"));
To copy to clipboard, switch view to plain text mode
I run the application and everything works fine. However, and what makes things very confusing, is that in MainWindow.qml, all the references to Box appear as errors, so:
import BasicComponents.Boxes 1.0
Box {
id: box
}
import BasicComponents.Boxes 1.0
Box {
id: box
}
To copy to clipboard, switch view to plain text mode
Would be underlined with a syntax error and the tooltip will pop up with, "unknown type".
Now I think I understand what is happening: the program works because it imports the plugin and the references to Box get resolved at runtime. But how can I make my plugin visible to QtCreator? I assume its possible since we use QtQuick and its modules are visible.
I feel there something important I'm missing because I'm assuming these plugins are for reusing modules from project to project and currently I don't see how this is done without the project having a local copy of the plugin?
I'll end this already long post here, I hope someone can help clarify this for me!
Bookmarks