Results 1 to 6 of 6

Thread: How to access a QList of QObject* in QML and Qt?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default How to access a QList of QObject* in QML and Qt?

    I need to have an array of class objects which should be accessible in Qt as well as QML.

    Considering http://stackoverflow.com/a/15830797/462608 following is what I have tried.

    Now I need to know how to read and write to the functions of ClassA through Qt and QML.

    See the print statement in the constructor in .h file. That produces a segmentation fault.

    class.h
    Qt Code:
    1. #ifndef CLASSA
    2. #define CLASSA
    3.  
    4. #include <QQuickItem>
    5.  
    6. class ClassA : public QObject
    7. {
    8. private:
    9. Q_OBJECT
    10.  
    11. Q_PROPERTY(int one READ one WRITE setOne NOTIFY oneChanged)
    12. int m_one;
    13.  
    14. public:
    15. ClassA () {}
    16. int one() const { return m_one; }
    17.  
    18. public slots:
    19. void setOne(int arg)
    20. {
    21. if (m_one == arg)
    22. return;
    23.  
    24. m_one = arg;
    25. emit oneChanged(arg);
    26. }
    27.  
    28. signals:
    29. void oneChanged(int arg);
    30. };
    31.  
    32. class ClassB : public QObject
    33. {
    34. private:
    35. Q_OBJECT
    36.  
    37. Q_PROPERTY(QList <QObject*> objClassAList READ objClassAList WRITE setObjClassAList NOTIFY objClassAListChanged)
    38. QList <QObject*> m_objClassAList;
    39.  
    40. public:
    41. ClassB ()
    42. {
    43. QList <QObject*> localObjList;
    44. for (int i = 0; i < 5; i++)
    45. {
    46. ClassA localObj;
    47. localObj.setOne (100);
    48. localObjList.push_back (&localObj);
    49. }
    50.  
    51. setObjClassAList (localObjList);
    52. qDebug () << "QQQQQQQQQQQQQQ: " << objClassAList ()[0]->property ("one");
    53. }
    54.  
    55. QList <QObject*> objClassAList () const { return m_objClassAList; }
    56.  
    57. public slots:
    58. void setObjClassAList (QList <QObject*> arg)
    59. {
    60. if (m_objClassAList == arg)
    61. return;
    62.  
    63. m_objClassAList = arg;
    64. emit objClassAListChanged(arg);
    65. }
    66.  
    67. signals:
    68. void objClassAListChanged (QList <QObject*> arg);
    69. };
    70.  
    71. #endif // CLASSA
    To copy to clipboard, switch view to plain text mode 

    main.cpp

    Qt Code:
    1. #include <QGuiApplication>
    2. #include <QQmlApplicationEngine>
    3. #include "class.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QGuiApplication app(argc, argv);
    8.  
    9. const char* what = "WHAT";
    10.  
    11. qmlRegisterType <ClassA> (what, 1, 0, "A");
    12. qmlRegisterType <ClassB> (what, 1, 0, "B");
    13.  
    14. QQmlApplicationEngine engine;
    15. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    16.  
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    main.qml
    Qt Code:
    1. import QtQuick 2.4
    2. import QtQuick.Window 2.2
    3. import WHAT 1.0
    4.  
    5. Window {
    6. visible: true
    7.  
    8. B
    9. {
    10. onObjClassAListChanged:
    11. {
    12.  
    13. }
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

  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 access a QList of QObject* in QML and Qt?

    Your code makes no sense because of several things the most important being that you are storing in a list pointers to local variables that go out of scope immediately afterwards (line #48 of the first snippet).

    Basically if you want a list of objects then the most straightforward solution is to use QQmlListProperty. Alternatively if your data is value based and not identity based then a simple property of type QVariantList (or even QVariant) should be enough. You can store data as QVariantMap which will be converted to a javascript object on the QML side. Since QVariantMap is itself QVariant, you can store it in QVariantList (which itself is a QVariant as well so the property can be of QVariant type). Don't follow what is written in that stack overflow thread, it's not a good solution, maybe apart the answer to use QAbstractListModel.
    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. The following user says thank you to wysota for this useful post:

    TheIndependentAquarius (29th January 2015)

  4. #3
    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: How to access a QList of QObject* in QML and Qt?

    Quote Originally Posted by wysota View Post
    Basically if you want a list of objects then the most straightforward solution is to use QQmlListProperty.
    IMHO this is only true if the requirement is to create objects on the QML side and put them into them list.

    A QList<QObject*> property is easier if the main objective is to export a list of objects from C++ to QML.

    However, given the context of the stackoverflow question (list of value objects), I would also suggest the QAbstractListModel subclass approach.

    Cheers,
    _

  5. #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 access a QList of QObject* in QML and Qt?

    Quote Originally Posted by anda_skoa View Post
    IMHO this is only true if the requirement is to create objects on the QML side and put them into them list.

    A QList<QObject*> property is easier if the main objective is to export a list of objects from C++ to QML.
    I would agree if there was no qmlRegisterType call for A. If that was on purpose and not an act of desperation when trying to find a solution, it would imply one wants the engine to understand the difference between QObject and A - the most obvious use-case being creating such objects in QML. Accessing A's methods and properties does not require registering the class in the engine.
    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.


  6. The following user says thank you to wysota for this useful post:

    TheIndependentAquarius (29th January 2015)

  7. #5
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to access a QList of QObject* in QML and Qt?

    Quote Originally Posted by wysota View Post
    Your code makes no sense because of several things the most important being that you are storing in a list pointers to local variables that go out of scope immediately afterwards (line #48 of the first snippet).
    Thankful for pointing out the fault and the additional information you've given regarding QVariant.
    Lack of concepts knowledge on my part.

    Quote Originally Posted by wysota View Post
    Don't follow what is written in that stack overflow thread, it's not a good solution, maybe apart the answer to use QAbstractListModel.
    You didn't explain why would using QObject* not be a good solution. Please explain as you would do to a layman.
    Also, in which case using QObject* would be considered a good solution?

    Quote Originally Posted by wysota View Post
    Accessing A's methods and properties does not require registering the class in the engine.
    I didn't understand the context here. You mean to say that I can access a C++ class in QMl without using qmlRegisterType?

  8. #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 access a QList of QObject* in QML and Qt?

    Quote Originally Posted by TheIndependentAquarius View Post
    You didn't explain why would using QObject* not be a good solution. Please explain as you would do to a layman.
    The thread at stackoverflow was about creating a kind of registry. It is quite limiting to only be able to read from the registry on the QML end. I don't know what your use-case is but you are trying to react on changes in the list. The list would only change if a new list was set on the property in C++. A model is a much better approach -- more consistent, less limitations albeit more difficult to implement than a simple list of objects.

    Also, in which case using QObject* would be considered a good solution?
    For example when you want to return a static list of objects and operate on them once in a while. That is what is bothering me in your case is that you want to react on when the list changes (of course that still could be ok depending on what you do with the data). But in general I assumed you would like to be creating instances of A (because of the register call in your code).

    I didn't understand the context here. You mean to say that I can access a C++ class in QMl without using qmlRegisterType?
    If it inherits QObject and you only want to access its methods and properties then you don't need qmlRegisterType. QObject is already registered. If you only need properties and if they are not being changed dynamically (I mean once set, they remain at their values) then it is simpler to use QVariant or a model.
    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.


Similar Threads

  1. Replies: 1
    Last Post: 14th March 2014, 22:40
  2. Replies: 0
    Last Post: 27th September 2013, 06:09
  3. SectionScroller and QList<QObject*>
    By marmistrz in forum Qt Quick
    Replies: 2
    Last Post: 3rd February 2013, 09:12
  4. Access violation -- qobject.cpp
    By willief in forum Newbie
    Replies: 9
    Last Post: 14th February 2011, 22:55
  5. QObject subclass in a QList
    By plan_rich in forum Newbie
    Replies: 3
    Last Post: 21st September 2010, 15:51

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.