Results 1 to 9 of 9

Thread: How to expose c++ plugins to Qml/QtCreator

  1. #1
    Join Date
    Jun 2011
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How to expose c++ plugins to Qml/QtCreator

    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.

    Qt Code:
    1. void BoxPlugin::registerTypes(const char *uri)
    2. {
    3. qmlRegisterType<Box>(uri, 1, 0, "Box");
    4. }
    To copy to clipboard, switch view to plain text mode 

    I then build the project with the following .pro file:

    Qt Code:
    1. TEMPLATE = lib
    2. CONFIG += qt plugin
    3. QT += declarative
    4.  
    5. DESTDIR = plugin
    6. TARGET = boxPlugin
    7.  
    8. ... headers and source files
    To copy to clipboard, switch view to plain text mode 

    Then in main.c I import the plugin:
    Qt Code:
    1. QPluginLoader loader("C:/Qt/myExamples/CppQmlProject/Src/Components/BasicComponents/plugin/boxPlugin.dll");
    2.  
    3. QDeclarativeExtensionPlugin *plugin = qobject_cast<QDeclarativeExtensionPlugin *>(loader.instance());
    4. if (plugin)
    5. plugin->registerTypes("BasicComponents.Boxes");
    6. else
    7. qDebug() << "FAIL!";
    8.  
    9. 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:

    Qt Code:
    1. import BasicComponents.Boxes 1.0
    2.  
    3. Box {
    4. id: box
    5. }
    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to expose c++ plugins to Qml/QtCreator

    Search the docs for "qmldir"
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2011
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to expose c++ plugins to Qml/QtCreator

    Hi wysota,

    I have a qmldir file, its found in C:\Qt\CppQmlProject\Src\Components\BasicComponent and it looks like:
    Qt Code:
    1. plugin boxPlugin /plugin
    To copy to clipboard, switch view to plain text mode 

    So it's pointing to the plugin dll which is found in the /plugin sub-directory.
    I then have my QML_IMPORT_PATH in the projects Build Environment set to C:\Qt\CppQmlProject\Src\Components\

    This I believes adds the import path as a location where the import statement looks. So When I:

    Qt Code:
    1. import BasicComponents
    To copy to clipboard, switch view to plain text mode 

    this is essentially appending BasicComponents to the import path, so we have: C:\Qt\CppQmlProject\Src\Components\BasicComponents

    which is where my qmldir file is. And should that resolve the references?

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to expose c++ plugins to Qml/QtCreator

    Are you sure the directory name should start with a slash? In general I believe a correct qmldir file and the plugin together should resolve the problem in Creator.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2011
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to expose c++ plugins to Qml/QtCreator

    So I think the plugin itself is ok because I am able to access its properties. There's also no way I am by-passing the QPluginLoader section either because without it the application fails to load.

    So there must be something wrong with either the qmldir file or perhaps its no even being read?

    It must be sufficient since the qdeclarativemodules simply says I need, plugin <Name> [<Path>] which you can see from above I have that (I removed the slash, it didn't change anything).

    Therefore QtCreator can't be reading it in the first place. So is what I've done correct? The plugin is not in the application directory. So I set the QML_IMPORT_PATH. By then attempting to import BasicComponents I'm then expecting QtCreator to look through all the import paths, appending the BasicComponents to the paths, then if there's a correct directory, then look for a qmldir file in that directory. Is that how the process works or have I gotten it wrong

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to expose c++ plugins to Qml/QtCreator

    Unfortunately I don't know such details. You'd have to look in Qt Creator source code to verify that. Also see if qmlviewer can detect your plugin using the qmldir file. If it works, Creator should detect it as well.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2011
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to expose c++ plugins to Qml/QtCreator

    Hi again,

    So I tried to load a qml file which contains a reference to the plugin and it doesn't work. What's more annoying is that when I edit the file with QtCreator its happy with the type!

    I've now copied the example found at http://qt.nokia.com/developer/learni...-quick-edition, downloaded the examples under the "Integrating QML with C++" heading and added the qml-cpp-integration/ex-extension-plugin to my project. I then build the project and try to load the standalone/ellipse9s.qml using the qmlviewer and again I get an error!

    Qt Code:
    1. file:///C:/Qt/myExamples/CppQmlProject/Src/Components/Elipse/standalone/ellipse9s.qml:8:5: Ellipse is not a type
    2. Ellipse {
    3. ^
    To copy to clipboard, switch view to plain text mode 

    Has anyone managed to get this example working?

    I've tried opening the file directly (open the viewer, then file, navigate to ellipse9s, open ellipse9s) and also from the command line with a -I to add the import path:

    Qt Code:
    1. qmlviewer -I qml-cpp-integration/ex-extension-plugin/standalone qml-cpp-integration/ex-extension-plugin/standalone/ellipse9s.qml
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to expose c++ plugins to Qml/QtCreator

    One thing to remember. Qt Creator is by default built against MSVC and Qt apps and probably your plugin as well is built against MinGW, so Creator might not be able to load it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jun 2011
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to expose c++ plugins to Qml/QtCreator

    I managed to get everything working by downloading the SDK and then following the steps mentioned here:http://developer.qt.nokia.com/forums/viewthread/5267
    (when I opened qmldump.pro QtCreator instantly highlighted the missing headers)

    Anyways I don't usually use the SDK and I tried to use the same fix for QtCreator, but it didn't appear to work - perhaps its the issue wysota mention,
    Qt Creator is by default built against MSVC and Qt apps and probably your plugin as well is built against MinGW, so Creator might not be able to load it.
    Haven't had time to look into this properly yet, but thanks for the suggestion!

Similar Threads

  1. Expose C++ enum to QML
    By OnlyK in forum Qt Quick
    Replies: 3
    Last Post: 3rd August 2011, 06:43
  2. Replies: 17
    Last Post: 2nd January 2011, 17:11
  3. QtCreator 2.0 crashes application working in QtCreator 1.3
    By been_1990 in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2010, 12:58
  4. Mac OS, plugins that user designer plugins
    By Royceybaby in forum Qt Programming
    Replies: 0
    Last Post: 9th May 2010, 01:40
  5. Qt Creator Qtcreator plugins
    By thereisnoknife in forum Qt Tools
    Replies: 0
    Last Post: 25th March 2010, 14:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.