Results 1 to 2 of 2

Thread: Subclassing QAbstractTableModel - Data from QSqlQuery

  1. #1
    Join Date
    Oct 2008
    Location
    Germany
    Posts
    29
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Subclassing QAbstractTableModel - Data from QSqlQuery

    Hi,

    I want to create a model that not only displays the data from an QSqlQuery but also changes the fields background-color. The color is passed by another query.

    I tried to do that by subclassing QAbstractTableModel.

    Here is my Code

    Qt Code:
    1. #ifndef MDL_STATUS_H
    2. #define MDL_STATUS_H
    3.  
    4. #include <QAbstractTableModel>
    5. #include <QSqlQuery>
    6.  
    7. class QStatusTableModel : public QAbstractTableModel {
    8. Q_OBJECT
    9. public:
    10. QStatusTableModel(const QSqlQuery &qry=QSqlQuery(), QObject *parent = 0)
    11. : QAbstractTableModel(parent), query(qry) {}
    12. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    13. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    14. QVariant data(const QModelIndex &index, int role) const;
    15. QVariant headerData(int section, Qt::Orientation orientation,
    16. int role = Qt::DisplayRole) const;
    17. void setQuery(const QSqlQuery &query);
    18. private:
    19. QSqlQuery query;
    20. };
    21. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mdl_status.h"
    2. #include <QSqlRecord>
    3.  
    4. int QStatusTableModel::rowCount(const QModelIndex &parent) const {
    5. return query.size();
    6. }
    7.  
    8. int QStatusTableModel::columnCount(const QModelIndex &parent) const {
    9. if (query.isValid() && query.size() > 0) {
    10. return query.record().count();
    11. }
    12. return 0;
    13. }
    14.  
    15. QVariant QStatusTableModel::data(const QModelIndex &index, int role) const {
    16. if (!index.isValid())
    17. return QVariant();
    18. if (index.row() >= query.size())
    19. return QVariant();
    20. if (role == Qt::DisplayRole) {
    21. query.seek(index.row());
    22. return query.value(index.column());
    23. } else {
    24. return QVariant();
    25. }
    26. }
    27.  
    28. QVariant QStatusTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
    29. if (role != Qt::DisplayRole)
    30. return QVariant();
    31. if (orientation == Qt::Horizontal)
    32. return QString("Column %1").arg(section);
    33. else
    34. return QString("Row %1").arg(section);
    35. }
    36.  
    37. void QStatusTableModel::setQuery(const QSqlQuery &qry) {
    38. query = qry;
    39. }
    To copy to clipboard, switch view to plain text mode 

    If I try to compile this code I get the Error:
    error: passing `const QSqlQuery' as `this' argument of `bool QSqlQuery::seek(int, bool)' discards qualifiers

    What do I do wrong? Is there another way to display what I want?

    Chris

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Subclassing QAbstractTableModel - Data from QSqlQuery

    nothing really.
    You just try to invoke a non-const method on a const object.
    Workarounds:
    * get rid of the const with const_cast
    *
    * read the query's result into a QList<QSqlRecord> or something that you can access in the data(), rowCount() etc methods without modifiying anything

Similar Threads

  1. Replies: 2
    Last Post: 27th April 2009, 15:15
  2. QAbstractTableModel , custom data
    By akon in forum Newbie
    Replies: 0
    Last Post: 17th April 2009, 16:03
  3. Best way to display lots of data fast
    By New2QT in forum Newbie
    Replies: 4
    Last Post: 16th October 2008, 22:46
  4. Replies: 7
    Last Post: 29th August 2008, 10:24
  5. Replies: 4
    Last Post: 19th October 2007, 19:47

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.