Results 1 to 8 of 8

Thread: Access QQmlListProperty<QObject> in qml

  1. #1
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Access QQmlListProperty<QObject> in qml

    Hi,

    I have a qt function of return type QQmlListProperty<QObject>.
    How can I use it in QML ?

    Thanks.

  2. #2
    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: Access QQmlListProperty<QObject> in qml

    That function should be associated as the READ function of a property.

    In QML you just use the property.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Access QQmlListProperty<QObject> in qml

    Thanks for the reply.

    Is it possible Without property ?

    Actually I am writing a class which will convert QList<QObject*>(QObject inherited class) to QAbstractListModel so that I can use it in QML.

    So I took a child class of QAbstractListModel and redefined the data function where I was reading the corresponding Properties.

    My QObject inherited sub class has a property of QQmlListProperty<QObject> type and now how to use it in QML side.

    For QList<QString> I am able to access by index in QML but for QQmlListProperty it is not working.

    Please help me or give some suggestion for the issues.

    Thanks.

  4. #4
    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: Access QQmlListProperty<QObject> in qml

    I am afraid I don't understand.

    Your object has a QQmlListProperty property but you don't want to use it?

    Can you post some code?

    Cheers,
    _

  5. #5
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Access QQmlListProperty<QObject> in qml

    Below I am giving one example.

    I have two QObject derived class "Country" and "State"

    State:
    Qt Code:
    1. class State : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    5. Q_PROPERTY(QList<QString> cmList READ cmList WRITE setCmList NOTIFY cmListChanged)
    6.  
    7. public:
    8.  
    9. QString name()
    10. {
    11. return m_name;
    12. }
    13.  
    14. void setName(QString val)
    15. {
    16. m_name = val;
    17. emit nameChanged();
    18. }
    19.  
    20. QList<QString> cmList()
    21. {
    22. return m_CMList;
    23. }
    24.  
    25. void setCmList(QList<QString> val)
    26. {
    27. m_CMList = val;
    28. emit cmListChanged();
    29. }
    30.  
    31. signals:
    32. void nameChanged();
    33. void cmListChanged();
    34.  
    35. private:
    36. QString m_name;
    37. QList<QString> m_CMList;
    38. };
    To copy to clipboard, switch view to plain text mode 

    Country:
    Qt Code:
    1. class Country : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    6. Q_PROPERTY(QList<QString> pmList READ pmList WRITE setPmList NOTIFY pmListChanged)
    7. Q_PROPERTY(QQmlListProperty<State> stateList READ stateList CONSTANT)
    8.  
    9. public :
    10.  
    11. Country()
    12. {
    13. }
    14.  
    15. QString name()
    16. {
    17. return m_name;
    18. }
    19.  
    20. void setName(QString val)
    21. {
    22. m_name = val;
    23. emit nameChanged();
    24. }
    25.  
    26. QList<QString> pmList()
    27. {
    28. return m_PMList;
    29. }
    30.  
    31. void setPmList(QList<QString> val)
    32. {
    33. m_PMList = val;
    34. emit pmListChanged();
    35. }
    36.  
    37. QQmlListProperty<State> stateList()
    38. {
    39. return QQmlListProperty<State>(this, m_StateList);
    40. }
    41.  
    42. void appendState(State* obj)
    43. {
    44. m_StateList.append(obj);
    45. }
    46.  
    47. signals:
    48. void nameChanged();
    49. void pmListChanged();
    50. private:
    51. QString m_name;
    52. QList<QString> m_PMList;
    53. QList<State*> m_StateList;
    54. };
    To copy to clipboard, switch view to plain text mode 

    Metatype:
    Qt Code:
    1. Q_DECLARE_METATYPE(QQmlListProperty< Country > )
    2. Q_DECLARE_METATYPE(QQmlListProperty< State > )
    To copy to clipboard, switch view to plain text mode 

    I also have "QAbstractListModel" derived model class called "MyModel"

    Qt Code:
    1. class MyModel : public QAbstractListModel
    2. {
    3. Q_OBJECT
    4. public:
    5. MyModel();
    6.  
    7. void setList(const QList<QObject*> &list)
    8. {
    9. m_list = list;
    10. }
    11.  
    12. int rowCount(const QModelIndex &parent) const
    13. {
    14. m_list.length();
    15. }
    16. int columnCount(const QModelIndex &parent) const
    17. {
    18. if(!m_list.isEmpty())
    19. return m_list.at(0)->metaObject()->propertyCount() - m_list.at(0)->metaObject()->propertyOffset();
    20. return 0;
    21. }
    22. QVariant headerData(int section, Qt::Orientation orientation, int role) const
    23. {
    24. Q_UNUSED(section);
    25. Q_UNUSED(orientation);
    26. Q_UNUSED(role);
    27. return QVariant();
    28. }
    29.  
    30. QHash<int, QByteArray> roleNames() const
    31. {
    32. QHash<int, QByteArray> roles;
    33.  
    34. if(!m_list.isEmpty())
    35. {
    36. const QMetaObject* metaObject = m_list.at(0)->metaObject();
    37. int r = Qt::UserRole+1;
    38.  
    39. for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i)
    40. {
    41. roles[r++] = QByteArray(metaObject->property(i).name());
    42. }
    43. }
    44.  
    45.  
    46. return roles;
    47. }
    48. QVariant data(const QModelIndex &index, int role) const
    49. {
    50. if (!index.isValid() || m_list.isEmpty())
    51. {
    52. return QVariant();
    53. }
    54. else
    55. {
    56. }
    57.  
    58. role = role - Qt::UserRole;
    59.  
    60.  
    61. if(role > 0 && m_list.length() > index.row())
    62. {
    63. const QMetaObject* metaObject = m_list.at(index.row())->metaObject();
    64.  
    65. role += metaObject->propertyOffset();
    66.  
    67. if(role <= metaObject->propertyCount())
    68. {
    69. return metaObject->property(role-1).read(m_list.at(index.row()));
    70. }
    71. }
    72.  
    73. return QVariant();
    74. }
    75. private:
    76. QList<QObject*> m_list;
    77. };
    To copy to clipboard, switch view to plain text mode 


    I have a QList<Country*> and I want to display it on my QML ListView.
    Below I gave a small example how i am using it.

    Qt Code:
    1. qmlRegisterType<Country>("mytest.country", 1, 0, "Country");
    2. qmlRegisterType<State>("mytest.state", 1, 0, "State");
    3.  
    4. QList<QString> cmList;
    5. QList<QString> pmList;
    6.  
    7. cmList << "cm1" << "cm2";
    8. pmList << "pm1" << "pm2";
    9.  
    10. State s1, s2, s3, s4;
    11.  
    12. s1.setName("S1");
    13. s1.setCmList(cmList);
    14.  
    15. s2.setName("S2");
    16. s2.setCmList(cmList);
    17.  
    18. s3.setName("S3");
    19. s3.setCmList(cmList);
    20.  
    21. s4.setName("S4");
    22. s4.setCmList(cmList);
    23.  
    24. Country c1, c2;
    25.  
    26. c1.setName("C1");
    27. c1.setPmList(pmList);
    28. c1.appendState(&s1);
    29. c1.appendState(&s2);
    30.  
    31. c2.setName("C2");
    32. c2.setPmList(pmList);
    33. c2.appendState(&s3);
    34. c2.appendState(&s4);
    35.  
    36. QList<QObject*> countryList;
    37.  
    38. countryList << &c1 << & c2;
    39.  
    40. MyModel myModel;
    41. myModel.setList(countryList);
    42.  
    43. QQmlApplicationEngine engine;
    44. QQmlContext * context = engine.rootContext();
    45.  
    46. context->setContextProperty("myModel", &myModel);
    47.  
    48. engine.load("../test/testqml.qml");
    To copy to clipboard, switch view to plain text mode 

    QML:
    Qt Code:
    1. import QtQuick 2.5
    2. import QtQuick.Controls 1.4
    3. import QtQuick.Controls.Styles 1.4
    4. import QtQuick.Window 2.2
    5. import QtQuick.Layouts 1.1
    6. import QtQml 2.0
    7.  
    8. import mytest.country 1.0
    9. import mytest.state 1.0
    10.  
    11. ApplicationWindow {
    12.  
    13. visible: true
    14. width: 600
    15. height: 600
    16.  
    17. Rectangle
    18. {
    19. id: rect1
    20. color: "transparent"
    21. anchors.fill: parent
    22.  
    23. anchors.verticalCenter: parent.verticalCenter
    24.  
    25.  
    26. ScrollView
    27. {
    28. id: slv1
    29.  
    30. width: parent.width
    31. height: parent.height
    32. contentItem: listView1
    33. focus: true
    34.  
    35. ListView
    36. {
    37. id: listView1
    38.  
    39. height: parent.height
    40. width: parent.width
    41. anchors.left: parent.left
    42. anchors.leftMargin: 10
    43. model: myModel
    44.  
    45. focus: true
    46. currentIndex: 0
    47.  
    48. delegate: Item{
    49. id: delegateRect
    50. width: listView1.width
    51. height: 80
    52.  
    53. Rectangle
    54. {
    55. width: listView1.width - 10
    56. height: listView1.height - 10
    57. color: "transparent"
    58.  
    59. Item
    60. {
    61. id: textIteml
    62. width: 150
    63. height:150
    64.  
    65. Text
    66. {
    67. anchors.verticalCenter: textIteml.verticalCenter
    68. color: "red"
    69. font.pointSize: 18
    70. text: name
    71. font.family: "corpoSLig"
    72. width: textIteml.width
    73. elide: Text.ElideRight
    74. }
    75.  
    76. }
    77.  
    78. Item
    79. {
    80. id: textIteml1
    81. width: 150
    82. height:150
    83. anchors.left: textIteml.right
    84. anchors.leftMargin: 5
    85.  
    86. Text
    87. {
    88. anchors.verticalCenter: parent.verticalCenter
    89. color: "red"
    90. font.pointSize: 18
    91. text: pmList[0]
    92. font.family: "corpoSLig"
    93. width: textIteml.width
    94. elide: Text.ElideRight
    95. }
    96.  
    97. }
    98.  
    99. Item
    100. {
    101. id: textIteml2
    102. width: 150
    103. height:150
    104. anchors.left: textIteml1.right
    105. anchors.leftMargin: 5
    106.  
    107. Text
    108. {
    109. anchors.verticalCenter: parent.verticalCenter
    110. color: "red"
    111. font.pointSize: 18
    112. text: stateList[0] //------------------------------Error : Unable to assign [undefined] to QString
    113. text: stateList //------------------------------Error : Unable to assign QQmlListProperty<State> to QString
    114. //How to print 1st state name
    115. font.family: "corpoSLig"
    116. width: textIteml.width
    117. elide: Text.ElideRight
    118. }
    119.  
    120. }
    121. }
    122.  
    123. }
    124.  
    125. }
    126. }
    127. }
    128.  
    129. }
    To copy to clipboard, switch view to plain text mode 


    My main target is there will be a single model class and it can convert any QList<QObjetc*> to model base on the properties.

    Thanks.

  6. #6
    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: Access QQmlListProperty<QObject> in qml

    Strange. I assume you have verified that the data() method actually returns the correct value, right?

    In any case the usage of QQmlListProperty in this context looks weird, there is no QML code that creates instances inside the list.
    This looks more like the use case of a QList<QObject*> or QList<State*> in this case.

    Some general observations:

    * your property setters lack the check for change, they always emit the notify signal even it the value did not change
    * not sure why you register State and Country as QML types, especially why in different modules?
    * are you aware that most QtQuick components can only deal with list models, not table models? I.e. columnCount == 1

    Cheers,
    _

  7. #7
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Access QQmlListProperty<QObject> in qml

    Strange. I assume you have verified that the data() method actually returns the correct value, right?
    I tried "QQmlListProperty AtFunction " and able to print correct data but after that my program was crashing.

    In any case the usage of QQmlListProperty in this context looks weird, there is no QML code that creates instances inside the list.
    This looks more like the use case of a QList<QObject*> or QList<State*> in this case.
    What I gave you is just an example. But my real classes are thrift generated classes and which is not in my control.
    I have to export those classes to Qml.

    your property setters lack the check for change, they always emit the notify signal even it the value did not change
    Thanks for the comment. Original implementation has the check for change. Here I just left all checks.

    are you aware that most QtQuick components can only deal with list models, not table models? I.e. columnCount == 1
    In our case there are some limitations(For nested QList we have to read only one index mostly 0)

    I don't have that much Knowledge regarding QtQuick/Qml. Can you please suggest some solutions for it so that I can display QList<QOblect> in QML ?

    Thanks.

  8. #8
    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: Access QQmlListProperty<QObject> in qml

    Quote Originally Posted by bibhukalyana View Post
    What I gave you is just an example. But my real classes are thrift generated classes and which is not in my control.
    I have to export those classes to Qml.
    Thrift as in Apache Thrift? Why does it generate QML specific properties, i.e. of type QQmlListProperty?

    Anyway, have you tried just returning the object itself from the model's data() function?

    Quote Originally Posted by bibhukalyana View Post
    Thanks for the comment. Original implementation has the check for change. Here I just left all checks.
    Ah, ok. Common mistake to forget that, so commented on it

    Quote Originally Posted by bibhukalyana View Post
    I don't have that much Knowledge regarding QtQuick/Qml. Can you please suggest some solutions for it so that I can display QList<QOblect> in QML ?
    All views can handle that as a model, e.g. ListView, but also Repeater, etc.

    Cheers,
    _

Similar Threads

  1. Replies: 4
    Last Post: 12th April 2015, 09:41
  2. How to access a QList of QObject* in QML and Qt?
    By TheIndependentAquarius in forum Qt Quick
    Replies: 5
    Last Post: 29th January 2015, 08:05
  3. QQmlListProperty Problem
    By sedi in forum Qt Quick
    Replies: 1
    Last Post: 5th October 2014, 02:33
  4. Access violation -- qobject.cpp
    By willief in forum Newbie
    Replies: 9
    Last Post: 14th February 2011, 23:55
  5. [Advanced] Qt Script access QObject from C++ code
    By bunjee in forum Qt Programming
    Replies: 5
    Last Post: 21st May 2009, 23:10

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.