Results 1 to 20 of 23

Thread: QML Plugin w/ QML resources

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2014
    Location
    Gelnhausen, Germany
    Posts
    21
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QML Plugin w/ QML resources

    Hello all,

    new to this form, and getting started with Qt/Quick. I am currently trying to factor out generic parts of our UI code into a plugin which would then provide new UI components usable in multiple projects. I chose the plugin path (over QML module), because deployment is a *little* less complicated (why in the world do I need to maintain the qmldir file? Why can't the plugin shared lib provide all info when loaded?), and since the code is fairly dynamic I prefer the typed environment of a C++ implementation over Javascript. However, I did not want to lose the advantages of the dedicated QML syntax. I therefore decided to include QML files as resources inside the plugin lib, load the components at runtime and create and parameterize items as required. In particular, I have a navigation panel which can show an arbitrary number of navigation tiles which will navigate to a configurable target.

    Now, here comes issue 1: I am able to load the QML component inside the componentComplete() method of my contributed type (call it NavigationPanel). I then try to find the GridLayout which is defined inside the resource file by id (findChild<QQuickItem *>(name)) - which fails.

    issue 2: I then try to get at the GridLayout using childItems().at(2), assuming that is the right one (theres only 2 nested items). I then go on to find nested NavigationTile objects (another contributed type) and use them to parameterize items created from another loaded QML component, and add the items to the GridLayout. When I start the application, nothing displays

    Heres my questions: is my approach even feasible? Why am I not able to retrieve the GridLayout child by id? What do I need to do to make everything display correctly? Do I need to implement updatePaintNode (and set QQuickItem::ItemHasContents), and if so, how?

    heres an example of the app code that uses my contributed types:

    Qt Code:
    1. Rectangle {
    2. NavigationPanel {
    3. NavigationTile {
    4. target: "nextpage1.qml"
    5. }
    6. NavigationTile {
    7. target: "nextpage2.qml"
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    and heres the QML that gets loaded from within the NavigationPanel implementation:

    Qt Code:
    1. Rectangle {
    2. Rectangle {
    3. GridLayout {
    4. id: tilesGrid
    5. }
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    I hope this makes sense to someone out there

    thanks,
    Christian

  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: QML Plugin w/ QML resources

    findChild returns objects by name based on objectName property, not their id. However using findChild to access some object like that seems really weird to me. I don't know what your elements do so it is hard for me to determine what you are trying to achieve. I just have an impression that you are overcomplicating things How does "NavigationTile" differ from Loader? What is its relation to NavigationPanel?
    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
    Jul 2014
    Location
    Gelnhausen, Germany
    Posts
    21
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QML Plugin w/ QML resources

    findChild returns objects by name based on objectName property, not their id
    huh? Never heard of that property. I am also not able to find any reference to it in the docs.
    How does "NavigationTile" differ from Loader? What is its relation to NavigationPanel?
    NavigationTile (and NavigationPanel, for that matter) behaves like Loader, in that it loads a QML component and creates an item from it. However, they reside in C++ code because I want to deploy a plugin (and dont want to use Javascript - tried to, didnt seem to work). Essentially, both types are wrappers that instatiate items from QML and parameterize them. NavigationTile describes the tiles nested inside the panel, corresponding to Rectangles inside the GridLayout

  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: QML Plugin w/ QML resources

    Quote Originally Posted by doulos View Post
    huh? Never heard of that property. I am also not able to find any reference to it in the docs.
    QObject::objectName

    NavigationTile (and NavigationPanel, for that matter) behaves like Loader, in that it loads a QML component and creates an item from it. However, they reside in C++ code because I want to deploy a plugin (and dont want to use Javascript - tried to, didnt seem to work). Essentially, both types are wrappers that instatiate items from QML and parameterize them. NavigationTile describes the tiles nested inside the panel, corresponding to Rectangles inside the GridLayout
    It seems an overkill to me to do it from within C++. If you have to have a C++ plugin then why not implement the code in QML, embed it into the C++ plugin using the resource system and then expose the types from within the plugin?
    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
    Jul 2014
    Location
    Gelnhausen, Germany
    Posts
    21
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QML Plugin w/ QML resources

    It seems an overkill to me to do it from within C++.
    it sure isn't totally convenient, but still better IMO than Javascript. And additionally, I get better deployability (one shared lib vs. multiple QML files)

    If you have to have a C++ plugin then why not implement the code in QML, embed it into the C++ plugin using the resource system and then expose the types from within the plugin?
    but that is exactly what I am trying to to! Now just tell me how, and I am done . Maybe you have an example I can look at?

    regards,
    Chris

  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: QML Plugin w/ QML resources

    Quote Originally Posted by doulos View Post
    it sure isn't totally convenient, but still better IMO than Javascript.
    If you extrapolate that then you will end up saying there is no sense in using QML at all.

    And additionally, I get better deployability (one shared lib vs. multiple QML files)
    As I said, those QML files can be embedded in the plugin library.

    but that is exactly what I am trying to to! Now just tell me how, and I am done . Maybe you have an example I can look at?
    I'd need to look for an example, but the general idea is to expose qml types with paths pointing to the resource system using a qmldir file.
    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
    Jul 2014
    Location
    Gelnhausen, Germany
    Posts
    21
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QML Plugin w/ QML resources

    If you extrapolate that then you will end up saying there is no sense in using QML at all.
    I dont agree. Right now I have the overall design nicely kept in QML, and I like it. Setting that up in C++ would be a nightmare. However, I am keeping the dymamic code in C++
    the general idea is to expose qml types with paths pointing to the resource system using a qmldir file.
    sorry to say, but that is way too general to be useful. I have already spent a couple of days working/investigation on this. I am also not sure that you have caught on to my problem: I am loading the QML file as a resource embedd within the shared lib, no qmldir involved. What I am looking for is an example of a C++ plugin which loads an embedded QML component and somehow exposes that as a custom type.

    thanks

  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: QML Plugin w/ QML resources

    The easiest way to do it is to have a regular directory structure containing qml files, put that all into qrc file and use QQmlEngine::addImportPath() where you will point to the qrc file. In my opinion having a C++ component whose only task is to expose a QML type that is already in a QML file makes no sense. If you already have a QML file and you want to expose it to QML then do that directly.
    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
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML Plugin w/ QML resources

    Just saying: if you are using findChild() to get access to an object defined in a QML scene, you are almost certainly doing something less than ideal.

    It creates a dependency of the C++ code toward the QML code, something one usually wants to avoid (goal being that the UI depends on the compiled code, not the other way around).

    Cheers,
    _

  10. #10
    Join Date
    Jul 2014
    Location
    Gelnhausen, Germany
    Posts
    21
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QML Plugin w/ QML resources

    In my opinion having a C++ component whose only task is to expose a QML type that is already in a QML file makes no sense
    agreed. My plugin does a lot more, this is just the reduction of one problem.

    I'll try again. Heres the QML interface I want to offer to my client:

    Qt Code:
    1. NavigationPanel {
    2. rows: 2
    3. columns: 2
    4. cellSpacing: 5
    5. cellPadding: 2
    6.  
    7. NavigationTile {
    8. color: "read"
    9. image: "some.png"
    10. columnSpan: 1
    11. rowSpan: 2
    12. target: "someother.qml"
    13. }
    14.  
    15. NavigationTile {
    16. color: "read"
    17. image: "some.png"
    18. target: "someother.qml"
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    now, internally, the QML representation for NavigationPanel and NavigationTile will be much more complex. NavigationPanel will consist of 2 nested Rectangles (the outer one being the one the navigation target item is placed into when a tile is clicked), and a nested GridLayout. The NavigationTile contains, among other things, a Loader object that will load the target page. The internal representations should not be exposed to the clients of my component, they should only need to write the code shown above.

    As I see it, implementing this will always involve a lot of imperative code, which I prefer to write in C++ (I actually tried Javascript, but got stuck)

  11. #11
    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: QML Plugin w/ QML resources

    I don't see a need for any complex imperative code here. I don't know what you have so far so let's try from scratch... BTW. I didn't test the code below, so it can contain minor errors but the general idea should be valid.

    NavigationPanel -- contains two rectangles and a grid layout:

    javascript Code:
    1. import QtQuick 2.2
    2. import QtQuick.Layouts 1.0
    3.  
    4. Item {
    5. id: root
    6.  
    7. property alias rows: grid.rows
    8. property alias columns: grid.columns
    9. property alias cellSpacing: grid.rowSpacing
    10. property int cellPadding: 0
    11.  
    12. default property list<NavigationTile> tiles
    13.  
    14. Rectangle {
    15. id: outer
    16.  
    17. Rectangle {
    18. id: inner
    19. }
    20. }
    21.  
    22. GridLayout {
    23. id: grid
    24. columnSpacing: rowSpacing
    25.  
    26. Repeater {
    27. model: root.tiles
    28.  
    29. Item {
    30. margins: root.cellPadding
    31. Image {
    32. anchors.fill: parent
    33. source: modelData.image
    34. }
    35. Rectangle {
    36. anchors.fill: parent
    37. color: modelData.color
    38. }
    39. Layout { rowSpan: modelData.rowSpan; columnSpan: modelData.columnSpan }
    40.  
    41. }
    42. }
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

    NavigationTile -- definition of a tile

    javascript Code:
    1. import QtQuick 2.2
    2.  
    3. QtObject {
    4. id: tile
    5.  
    6. property color color
    7. property string image
    8. property int columnSpan: 1
    9. property int rowSpan: 1
    10. property string target
    11. }
    To copy to clipboard, switch view to plain text mode 

    What's next?
    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.


  12. #12
    Join Date
    Jul 2014
    Location
    Gelnhausen, Germany
    Posts
    21
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QML Plugin w/ QML resources

    looks not bad, actually . At least at first sight I can see how this might fit my bill. It seems that wrapping ones head around this paradigm is not as easy as one would think. There are several things in your code which can understand when I read them, but don't see how I would have come up with them. I am not in my Qt office right now, so I will try it out tomorrow and report. Thanks much so far

    What's next?
    what if I insisted on doing this in C++? Just joking..

Similar Threads

  1. Is it possible to add QT resources to a DLL?
    By cboles in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2017, 00:12
  2. Replies: 19
    Last Post: 12th June 2014, 07:30
  3. Plugin embedded resources fails to load
    By deadbird in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2011, 21:09
  4. Replies: 1
    Last Post: 8th October 2010, 11:38
  5. Qt Resources
    By kaushal_gaurav in forum Qt Programming
    Replies: 3
    Last Post: 3rd October 2008, 16:30

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.