Results 1 to 1 of 1

Thread: Custom model for QTreeView with SQLite data source

  1. #1
    Join Date
    Nov 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Custom model for QTreeView with SQLite data source

    Hello,

    I'm getting strange qDebug behaviour. Here is the code:

    Qt Code:
    1. // TreeModel.hpp
    2. #ifndef TREEMODEL_HPP
    3. #define TREEMODEL_HPP
    4.  
    5. #include <QAbstractItemModel>
    6.  
    7. class TreeModel : public QAbstractItemModel
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit TreeModel(QObject *parent = 0);
    13. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    14. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    15. QVariant data(const QModelIndex &item, int role) const;
    16. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    17. Qt::ItemFlags flags(const QModelIndex &index) const;
    18. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
    19. QModelIndex parent(const QModelIndex &index) const;
    20.  
    21. };
    22.  
    23. #endif // TREEMODEL_HPP
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // TreeModel.cpp
    2. #include <QSqlQuery>
    3. #include <QSqlError>
    4. #include <QVector>
    5. #include <QMessageBox>
    6.  
    7. #include "TreeModel.hpp"
    8.  
    9. TreeModel::TreeModel(QObject *parent) :
    10. {
    11. }
    12.  
    13. int TreeModel::rowCount(const QModelIndex &parent) const
    14. {
    15. // top level items
    16. if(!parent.isValid())
    17. {
    18. QSqlQuery q1("SELECT id FROM table_a");
    19.  
    20. // QSQLITE doesn't support size() function
    21. int size = 0;
    22. while(q1.next())
    23. size++;
    24.  
    25. return size;
    26. }
    27.  
    28. // children
    29. if(parent.internalId() == 0)
    30. {
    31. q2.prepare("SELECT id FROM table_b WHERE table_a_id = :table_a_id");
    32. q2.bindValue(":table_a_id", parent.data().toInt());
    33. q2.exec();
    34.  
    35. // QSQLITE doesn't support size() function
    36. int size = 0;
    37. while(q2.next())
    38. size++;
    39.  
    40. return size;
    41. }
    42.  
    43. return 0;
    44. }
    45.  
    46. int TreeModel::columnCount(const QModelIndex &parent) const
    47. {
    48. Q_UNUSED(parent);
    49.  
    50. return 1;
    51. }
    52.  
    53.  
    54. QVariant TreeModel::data(const QModelIndex &item, int role) const
    55. {
    56. if(!item.isValid())
    57. return QVariant();
    58.  
    59. if(role == Qt::DisplayRole)
    60. {
    61. if(item.internalId() == 0)
    62. {
    63. // top level item...
    64.  
    65. if(item.column() == 0)
    66. {
    67. QSqlQuery q1("SELECT id FROM table_a ORDER BY id");
    68.  
    69. // QSQLITE doesn't like seek() function
    70. int r = 0;
    71. while(q1.next())
    72. {
    73. if(r == item.row())
    74. return q1.value(0).toInt();
    75. r++;
    76. }
    77. }
    78. }
    79. else
    80. {
    81. // child...
    82.  
    83. q2.prepare("SELECT id FROM table_b WHERE table_a_id = :table_a_id ORDER BY id");
    84. q2.bindValue(":table_a_id", item.parent().data().toInt());
    85. q2.exec();
    86.  
    87. // QSQLITE doesn't like seek() function
    88. int r = 0;
    89. while(q2.next())
    90. {
    91. if(r == item.row())
    92. return q2.value(0).toInt();
    93. r++;
    94. }
    95. }
    96. }
    97.  
    98. return QVariant();
    99. }
    100.  
    101.  
    102. QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
    103. {
    104. if(orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0)
    105. return QString("Id");
    106.  
    107. return QVariant();
    108. }
    109.  
    110.  
    111. Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
    112. {
    113. Q_UNUSED(index);
    114.  
    115. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    116. }
    117.  
    118.  
    119. QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
    120. {
    121. if(!parent.isValid())
    122. return createIndex(row, column, 0);
    123.  
    124. return createIndex(row, column, 1);
    125. }
    126.  
    127.  
    128. QModelIndex TreeModel::parent(const QModelIndex &index) const
    129. {
    130. if(index.isValid())
    131. {
    132. if(index.internalId() == 0)
    133. return QModelIndex();
    134. else
    135. {
    136. // this line is fine
    137. qDebug("internalId != 0");
    138.  
    139. // this line is missing...
    140. qDebug("data: %i", index.data().toInt());
    141.  
    142. /*
    143.  
    144.   return valid QModelIndex...
    145.  
    146.   */
    147. }
    148. }
    149.  
    150. return QModelIndex();
    151. }
    To copy to clipboard, switch view to plain text mode 

    Line 143 in TreeModel.cpp doesn't print any output. I was working on parent() function and I discovered that something is wrong with index.data(). I've added qDebug() and... surprise! - qDebug() gives no output. What am I missing here?
    Attached Files Attached Files

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 20:39
  2. Replies: 0
    Last Post: 16th November 2010, 10:55
  3. Flat data source for QTreeView/Model
    By mawt in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2009, 17:20
  4. Modify model data in QTreeView
    By YuriyRusinov in forum Qt Programming
    Replies: 6
    Last Post: 26th October 2006, 18:28
  5. [QT4 & XP] retrieve data from QTreeView's model
    By incapacitant in forum Newbie
    Replies: 1
    Last Post: 2nd March 2006, 14:02

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.