Results 1 to 5 of 5

Thread: Show complex cpp data model in QML

  1. #1
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Show complex cpp data model in QML

    i have tow c++ data model like this :
    post data model:
    Qt Code:
    1. class post_data_model : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit post_data_model(QObject *parent = 0);
    7.  
    8. Q_PROPERTY(QString key READ key WRITE set_key)
    9. Q_PROPERTY(QString text READ text WRITE set_text)
    10. Q_PROPERTY(QString raw_text READ raw_text WRITE set_raw_text)
    11. Q_PROPERTY(QVariant tags READ tags)
    12.  
    13. void set_key(QString key);
    14. void set_text(QString text);
    15. void set_raw_text(QString raw_text);
    16. void set_tags(QList <QObject *> tags);
    17.  
    18. QString key();
    19. QVariant tags();
    20. QString text();
    21. QString raw_text();
    22.  
    23. private:
    24. QString m_key;
    25. QList <QObject *> m_tags;
    26. QString m_text;
    27. QString m_raw_text;
    28. }
    To copy to clipboard, switch view to plain text mode 

    and,tag data model :
    Qt Code:
    1. class tags_model : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit tags_model(QObject *parent = 0);
    6.  
    7. Q_PROPERTY(QString key READ key WRITE set_key)
    8. Q_PROPERTY(QString name READ name WRITE set_name)
    9.  
    10. void set_key(QString key);
    11. void set_name(QString name);
    12.  
    13. QString key();
    14. QString name();
    15. private:
    16. QString m_key;
    17. QString m_name;
    To copy to clipboard, switch view to plain text mode 

    QML Code :
    Qt Code:
    1. import QtQuck 2.2
    2. import QtQuick.Window 2.1
    3. import QtQuick.Controls 1.2
    4.  
    5. Window {
    6. id:container_window
    7. visible: true
    8. width: 360
    9. height: 360
    10. color: "#E6E7E7"
    11. property bool get_profile_status :false
    12. onActiveChanged:
    13. {
    14. if (get_profile_status==false)
    15. {
    16. Posts_Action.get_posts_stream();
    17. get_profile_status=true
    18. }
    19.  
    20. }
    21.  
    22. ListView
    23. {
    24. id:posts_list_view
    25. height: 200
    26. width: 200
    27. spacing: 5
    28. clip: true
    29. delegate: posts_delegate
    30. }
    31.  
    32.  
    33. Component
    34. {
    35. id: posts_delegate
    36.  
    37. Item {
    38. height: 150
    39.  
    40. Text {
    41. id: post_content_txt
    42. width: 200
    43. wrapMode: Text.WordWrap
    44. anchors.topMargin: 10
    45. text: qsTr(modelData.text)
    46. }
    47.  
    48. //Tags : No idea
    49. //How Can show list of tags?
    50. }
    51. }
    52.  
    53. Item {
    54. id: exteras
    55. Connections
    56. {
    57. target:Posts_Action
    58. onParsfinished:
    59. {
    60. posts_list_view.model=Posts_Action.post_list();
    61. }
    62. onNetwork_error_occurred :
    63. {
    64. console.log("ERROR: " + error_message);
    65. }
    66. }
    67. }
    To copy to clipboard, switch view to plain text mode 

    i can use the property of post model in QML such as the key and text but the point is that i cannot have access to the list of tags...
    now,how can i show the list of post tags in QML?
    Last edited by ms2222; 18th February 2015 at 10:45.

  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: Show complex cpp data model in QML

    Doesn't modelData.tags contain the data you want?
    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
    Mar 2012
    Posts
    6
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Show complex cpp data model in QML

    Doesn't modelData.tags contain the data you want?
    i can't exactly understand your purpose...
    tags is a list of data not single item,if modelData.tags return the list,now how can i use it,for example how show it under the post text?
    Last edited by ms2222; 18th February 2015 at 11:07.

  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: Show complex cpp data model in QML

    Set it as a model of another listview or repeater.

    javascript Code:
    1. import QtQuick 2.0
    2.  
    3. ListView {
    4. id: view
    5.  
    6. ListModel {
    7. id: listModel
    8. ListElement {
    9. text: "Item1"
    10. tags: "tag1,tag2,tag3"
    11. }
    12. ListElement {
    13. text: "Item2"
    14. tags: "tag4,tag5"
    15. }
    16. }
    17.  
    18. model: listModel
    19.  
    20. delegate: Column {
    21. width: view.width
    22. property var tags: model.tags.split(",")
    23. Text { width: parent.width; text: model.text; font.pixelSize: 24 }
    24. Flow {
    25. width: view.width
    26. spacing: 4
    27. Repeater {
    28. model: tags
    29. Text { text: tags[index] }
    30. }
    31. }
    32.  
    33. }
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 18th February 2015 at 11:18.
    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. The following user says thank you to wysota for this useful post:

    ms2222 (18th February 2015)

  6. #5
    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: Show complex cpp data model in QML

    General rule: if you have Q_PROPERTY entries that you use from QML, make sure they have NOTIFY signals.

    Cheers,
    _

Similar Threads

  1. Using model indices in complex model item relationships
    By hackerNovitiate in forum Newbie
    Replies: 0
    Last Post: 29th June 2011, 15:30
  2. Printing complex page with changing data
    By marcvanriet in forum Qt Programming
    Replies: 4
    Last Post: 22nd December 2010, 02:15
  3. Replies: 1
    Last Post: 23rd November 2008, 15:11
  4. Something fishy about Model, data doesn't show up!
    By zlosynus in forum Qt Programming
    Replies: 4
    Last Post: 4th August 2008, 16:07
  5. Model, view, resize to show all data
    By jcr in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2006, 21:59

Tags for this Thread

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.