Results 1 to 8 of 8

Thread: QsqlQueryModel Subclass

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QsqlQueryModel Subclass

    Here is an example
    ProxyModel.jpg
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include <QApplication>
    4.  
    5. // Add one extra column at the beginning
    6. class ProxyModel : public QAbstractProxyModel
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit ProxyModel(QObject * parent = 0)
    11. {
    12. ;
    13. }
    14.  
    15. void setSourceModel(QAbstractItemModel *sourceModel)
    16. {
    17. if(this->sourceModel() != sourceModel)
    18. {
    19. if(this->sourceModel() != 0)
    20. {
    21. disconnect(this->sourceModel(), SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
    22. this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)));
    23. disconnect(this->sourceModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
    24. this, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
    25. disconnect(this->sourceModel(), SIGNAL(columnsAboutToBeInserted(const QModelIndex &, int, int)),
    26. this, SIGNAL(columnsAboutToBeInserted (const QModelIndex &, int, int)));
    27. disconnect(this->sourceModel(), SIGNAL(columnsInserted(const QModelIndex &, int, int)),
    28. this, SIGNAL(columnsInserted(const QModelIndex &, int, int)));
    29. disconnect(this->sourceModel(), SIGNAL(modelAboutToBeReset()),
    30. this, SIGNAL(modelAboutToBeReset()));
    31. disconnect(this->sourceModel(), SIGNAL(modelReset()),
    32. this, SIGNAL(modelReset()));
    33. disconnect(this->sourceModel(), SIGNAL(layoutAboutToBeChanged()),
    34. this, SIGNAL(layoutAboutToBeChanged()));
    35. disconnect(this->sourceModel(), SIGNAL(layoutChanged()),
    36. this, SIGNAL(layoutChanged()));
    37. disconnect(this->sourceModel(), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
    38. this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)));
    39. }
    40.  
    41. QAbstractProxyModel::setSourceModel(sourceModel);
    42.  
    43. connect(this->sourceModel(), SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)),
    44. this, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)));
    45. connect(this->sourceModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
    46. this, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
    47. connect(this->sourceModel(), SIGNAL(columnsAboutToBeInserted (const QModelIndex &, int, int)),
    48. this, SIGNAL(columnsAboutToBeInserted(const QModelIndex &, int, int)));
    49. connect(this->sourceModel(), SIGNAL(columnsInserted(const QModelIndex &, int, int)),
    50. this, SIGNAL(columnsInserted(const QModelIndex &, int, int)));
    51. connect(this->sourceModel(), SIGNAL(modelAboutToBeReset()),
    52. this, SIGNAL(modelAboutToBeReset()));
    53. connect(this->sourceModel(), SIGNAL(modelReset()),
    54. this, SIGNAL(modelReset()));
    55. connect(this->sourceModel(), SIGNAL(modelReset()),
    56. this, SIGNAL(modelReset()));
    57. connect(this->sourceModel(), SIGNAL(layoutAboutToBeChanged()),
    58. this, SIGNAL(layoutAboutToBeChanged()));
    59. connect(this->sourceModel(), SIGNAL(layoutChanged()),
    60. this, SIGNAL(layoutChanged()));
    61. connect(this->sourceModel(), SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)),
    62. this, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)));
    63. }
    64. }
    65.  
    66. QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const
    67. {
    68. if(!parent.isValid())
    69. {
    70. if(column == 0)
    71. return createIndex(row, column, -1);
    72. else
    73. return createIndex(row, column, 0);
    74. }
    75. return QModelIndex();
    76. }
    77.  
    78. QModelIndex parent(const QModelIndex & /*child*/) const
    79. {
    80. return QModelIndex();
    81. }
    82.  
    83. int rowCount(const QModelIndex & parent = QModelIndex()) const
    84. {
    85. if(!parent.isValid())
    86. return sourceModel()->rowCount(parent);
    87. return 0;
    88. }
    89.  
    90. int columnCount(const QModelIndex & parent = QModelIndex()) const
    91. {
    92. if(!parent.isValid())
    93. return sourceModel()->columnCount(parent) + 1;
    94. return 0;
    95. }
    96.  
    97. QModelIndex mapToSource(const QModelIndex & proxyIndex) const
    98. {
    99. if(proxyIndex.column() <= 0)
    100. return QModelIndex();
    101.  
    102. return sourceModel()->index(proxyIndex.row(), proxyIndex.column() - 1);
    103. }
    104.  
    105. QModelIndex mapFromSource(const QModelIndex & sourceIndex) const
    106. {
    107. return index(sourceIndex.row(), sourceIndex.column() + 1);
    108. }
    109.  
    110. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
    111. {
    112. if(orientation == Qt::Horizontal)
    113. {
    114. if(section <= 0)
    115. return QString("Proxy Column");
    116. return sourceModel()->headerData(section - 1, orientation, role);
    117. }
    118. return sourceModel()->headerData(section, orientation, role);
    119. }
    120.  
    121. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
    122. {
    123. if((index.column() <= 0) and (role == Qt::DisplayRole))
    124. return QString("Proxy Data - Row:%1").arg(index.row());
    125.  
    126. return sourceModel()->data(mapToSource(index), role);
    127. }
    128.  
    129. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)
    130. {
    131. if((index.column() <= 0) and (role == Qt::EditRole))
    132. {
    133. // TODO: store data into ProxyModel
    134. return true;
    135. }
    136. return sourceModel()->setData(mapToSource(index), role);
    137. }
    138. };
    139.  
    140. int main(int argc, char *argv[])
    141. {
    142. QApplication app(argc, argv);
    143.  
    144. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    145. db.setDatabaseName("Qt.sqlite");
    146.  
    147.  
    148. ProxyModel proxy;
    149. proxy.setSourceModel(&model);
    150.  
    151. QTableView view;
    152. view.setWindowTitle("QSqlQueryModel View");
    153. view.setModel(&model);
    154. view.show();
    155.  
    156. QTableView proxyView;
    157. proxyView.setWindowTitle("ProxyModel View");
    158. proxyView.setModel(&proxy);
    159. proxyView.show();
    160.  
    161. if(db.open())
    162. model.setQuery("SELECT Country, Location FROM Locations");
    163.  
    164. view.resizeColumnsToContents();
    165. proxyView.resizeColumnsToContents();
    166.  
    167. return app.exec();
    168. }
    169.  
    170. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  2. The following user says thank you to Santosh Reddy for this useful post:

    Delphi (15th May 2013)

Similar Threads

  1. Replies: 2
    Last Post: 15th April 2013, 06:33
  2. QSqlQueryModel
    By StefanLatsch in forum Qt Programming
    Replies: 1
    Last Post: 7th December 2010, 11:09
  3. Does 'QSqlQueryModel' have a bug?
    By nuntawat in forum Qt Programming
    Replies: 8
    Last Post: 6th April 2010, 17:45
  4. Replies: 8
    Last Post: 12th February 2010, 02:41
  5. QSqlQueryModel write subclass
    By skuda in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 16:18

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
  •  
Qt is a trademark of The Qt Company.