Results 1 to 3 of 3

Thread: setData in a model derived from QAbstractTableModel

  1. #1
    Join Date
    Aug 2009
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default setData in a model derived from QAbstractTableModel

    I expect setData() below to store the string somewhere in the model but the call fails. Do I do something wrong or do I misunderstand what setData() is doing. Same for setHeaderdata()

    The simplest code that fails to build the model as I expect is a header file .hpp

    Qt Code:
    1. #include <QAbstractTableModel>
    2.  
    3. class Model : public QAbstractTableModel
    4. {
    5. Q_OBJECT
    6. public:
    7. Model(QObject *parent = 0) : QAbstractTableModel(parent) {};
    8.  
    9. void setSize(int r, int c) {
    10. rows = r;
    11. cols = c;
    12. }
    13.  
    14. int rowCount(const QModelIndex &parent = QModelIndex()) const { return rows; };
    15. int columnCount(const QModelIndex &parent = QModelIndex()) const { return cols; };;
    16.  
    17. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
    18. { return QVariant(); };
    19. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
    20. { return QVariant(); };
    21.  
    22. private:
    23. int rows, cols;
    24. };
    To copy to clipboard, switch view to plain text mode 

    and a source file .cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <trial.hpp>
    3.  
    4. int main(int argc, char* argv[]) {
    5. Model* model = new Model();
    6. model->setSize(8,6);
    7.  
    8. QVariant *v = new QVariant(QString("Some text"));
    9. if( ! model->setHeaderData(0, Qt::Horizontal, *v) )
    10. qDebug() << "Set hdr failed";
    11.  
    12. v = new QVariant(QString("Some more text"));
    13. QModelIndex mi = model->index(2,4);
    14. if( ! model->setData(mi, *v) )
    15. qDebug() << "Set data failed";
    16. }
    To copy to clipboard, switch view to plain text mode 
    Any help most welcome,
    Enno

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: setData in a model derived from QAbstractTableModel

    QAbstractTableModel inherits setData() from QAbstractItemModel. From the docs for QAbstractItemModel::setData():
    Sets the role data for the item at index to value.
    Returns true if successful; otherwise returns false.
    The dataChanged() signal should be emitted if the data was successfully set.
    The base class implementation returns false. This function and data() must be reimplemented for editable models.

  3. #3
    Join Date
    Aug 2009
    Posts
    24
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setData in a model derived from QAbstractTableModel

    The above very indirectly solved my problem I mistakenly thought setData() would store data inside the model.

    I now realize that I myself have to define the data structure as member of the model to keep data for cells. Programmatically I fill that data structure.
    The view uses data() to get at this data. In the case of a TableModel rowCount() and columnCount() simply return the number of rows and columns of that structure.

    Only for an editable model is a setData() function required to update the data for a cell as only I know what structure the data is kept in

    The obvious is sometimes difficult to see.

Similar Threads

  1. Slow update in QAbstractTableModel in setData
    By cevou in forum Qt Programming
    Replies: 2
    Last Post: 20th May 2010, 14:04
  2. QBstractTableMode derived model, not showing in QTableView
    By high_flyer in forum Qt Programming
    Replies: 4
    Last Post: 24th March 2010, 15:04
  3. setData()
    By starcontrol in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2008, 08:54
  4. model/view call setdata() function help?
    By dolphins in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2007, 01:58
  5. model/view setData help
    By nategoofs in forum Qt Programming
    Replies: 16
    Last Post: 19th August 2007, 18:00

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.