Results 1 to 4 of 4

Thread: QTableView Model Help

  1. #1
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTableView Model Help

    Hi,

    I'm having some problems working out how to implement the data() function from QAbstractTableModel for use in a QTableView model.

    My data is stored in a large vector, that the model (which is read only) has a pointer to, I set up the headerData() function, so all the column names are correct and that works fine. rowCount and ColumnCount are also in my class, correct i think.

    I have 4 columns, and x number or rows, so i just need some help with returning the correct data from the data() function.

    Here is my code, im basically not sure how i use the index and role values in order to know a) which column the data is from, and b) the row number. I need these 2 values in order to know which data to get from the data vector. For example if its from column 2 and row 5, i know i need to return the subject from element 5 in the vector. Is it as simple as role is the column and index is the row?

    Qt Code:
    1. QVariant QueueModel::data(const QModelIndex &index, int role) const
    2. {
    3. if (!index.isValid() || role != Qt::DisplayRole)
    4. return QVariant();
    5.  
    6. //return ....
    7. }
    To copy to clipboard, switch view to plain text mode 

    Also, do i have to do anything special to keep the view updating? i.e if i change a value in the data vector and another point in my program (outside of the model), will the view just update automatically?

    Thanks alot for any tips,

    Jack
    Last edited by tntcoda; 22nd June 2008 at 15:33.

  2. #2
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: QTableView Model Help

    Hi,

    you can use index.row() and index.column().

    If you want to return "default" values you return
    Qt Code:
    1. return QSqlQueryModel(or the base class of your model)::data(index, role)
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView Model Help

    Thanks alot, that makes sense so i can just return a QString from data() depending on the row/col which is great.

    It seems though that the data() function isnt being called at all? The model has 0 rows to start with, just columns so i suppose there is no need to get any data.

    However when i add data to the vector from another class, the model/view needs to update and request data, any pointers on how i can do this?

    This is my model class:
    Qt Code:
    1. class QueueModel : public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. QueueModel(QWidget * parent, queue *q);
    7.  
    8. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    9. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    10.  
    11. // Returns data for specified index
    12. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    13.  
    14. // For putting labels on header columns
    15. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    16.  
    17. private:
    18. queue *que; // rows = que->GetCount()
    19. int colCount; // Number of columns
    20. };
    To copy to clipboard, switch view to plain text mode 

    Problem is data is never called
    Last edited by tntcoda; 22nd June 2008 at 17:02.

  4. #4
    Join Date
    Jul 2007
    Location
    Germany
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView Model Help

    Quote Originally Posted by tntcoda View Post
    Thanks alot, that makes sense so i can just return a QString from data() depending on the row/col which is great.

    It seems though that the data() function isnt being called at all? The model has 0 rows to start with, just columns so i suppose there is no need to get any data.

    However when i add data to the vector from another class, the model/view needs to update and request data, any pointers on how i can do this?

    Problem is data is never called
    Hello, you could try to add the data by using the setData() method of your model (you would have to reimplement it). It that method, you then emit the dataChanged() signal to update your view.

    Arghargh

Similar Threads

  1. Replies: 6
    Last Post: 22nd November 2011, 03:53
  2. QTableView sorting when using a model
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 13:13
  3. Help choosing a model type for QTableView
    By goes2bob in forum Newbie
    Replies: 1
    Last Post: 10th March 2008, 02:22
  4. Replies: 5
    Last Post: 16th May 2007, 11:03
  5. Replies: 9
    Last Post: 7th November 2006, 15:10

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.