Results 1 to 2 of 2

Thread: TableView and QAbstractTableModel

  1. #1
    Join Date
    Sep 2013
    Posts
    9
    Thanked 1 Time in 1 Post

    Default TableView and QAbstractTableModel

    Hi,

    I've got a problem to get the TableView (the qtquick control) to work with my c++ model class (subclassed from QAbstractTableModel).

    my qml file (or part of it) looks like this:

    Qt Code:
    1. TableView
    2. {
    3. anchors.margins: 12;
    4. anchors.fill: parent;
    5. TableViewColumn
    6. {
    7. role: model.jobID;
    8. title: "Job ID";
    9. width: 120;
    10. }
    11. TableViewColumn
    12. {
    13. role: model.jobStatus;
    14. title: "Job Status";
    15. width: 120;
    16. }
    17. model: operatorMainStateQMLInterface.model;
    18. }
    To copy to clipboard, switch view to plain text mode 

    my subclassed QAbstractTableModel looks like this (with added inlines to keep it shorter)
    Qt Code:
    1. class AMSJobsListModel: public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4.  
    5. enum AMSJobsListModelRoles
    6. {
    7. JobIDRole = Qt::UserRole + 1,
    8. JobStatusRole
    9. };
    10.  
    11. public:
    12. AMSJobsListModel(QObject *parent = 0);
    13. virtual ~AMSJobsListModel(void);
    14. public:
    15.  
    16. virtual int rowCount (const QModelIndex &parent = QModelIndex()) const
    17. {
    18. if (!parent.isValid())
    19. {
    20. (int)_jobs.size();
    21. }
    22. else
    23. {
    24. return 0;
    25. }
    26. }
    27.  
    28. virtual int columnCount (const QModelIndex &parent = QModelIndex()) const
    29. {
    30. return 2;
    31. }
    32. QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    33. {
    34. if (index.row() < 0 || index.row() >= _jobs.size())
    35. return QVariant();
    36.  
    37. if (role == JobIDRole)
    38. return _jobs[index.row()]->getJobID();
    39. else if (role == JobStatusRole)
    40. return _jobs[index.row()]->getJobStatus();
    41. return QVariant();
    42. }
    43.  
    44. QHash<int, QByteArray> roleNames() const
    45. {
    46. QHash<int, QByteArray> roles;
    47. roles[JobIDRole] = "jobID";
    48. roles[JobStatusRole] = "jobStatus";
    49. return roles;
    50. }
    51.  
    52. private:
    53. std::vector<boost::shared_ptr<AMSJob> > _jobs;
    54. };
    To copy to clipboard, switch view to plain text mode 

    The idea is that I have a table with in the columns JobID and JobStatus (and later other items)
    When I remove the JobStatusRole, everything seems to work fine (one column with the jobid's in it), when i introduce the jobstatusrole, not even the data() function is called (a breakpoint in the method is not hit by the debugger). When I make two columns in the table view without the jobstatusrole, in both columns the jobid is shown. I'm getting a little bit lost with the roles (I assume I don't understand the concept completely) Anybody got an idea or can point me in the right direction?
    Cheers,

    Matt

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


  3. #2
    Join Date
    Sep 2013
    Posts
    9
    Thanked 1 Time in 1 Post

    Default Re: TableView and QAbstractTableModel

    Ok, I have found it myself. Apparently, in the tableview, the role property should really be a string ( role :"jobStatus", not role: model.jobStatus). Stupid...
    cheers,

    Matt

Similar Threads

  1. Replies: 8
    Last Post: 29th November 2012, 13:19
  2. QAbstractTableModel and tooltip
    By mpi in forum Qt Programming
    Replies: 1
    Last Post: 29th March 2010, 19:58
  3. QAbstractTableModel and sort
    By foxyproxy in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 09:30
  4. Using QAbstractTableModel.parent
    By Max Yaffe in forum Newbie
    Replies: 7
    Last Post: 15th June 2007, 15:21
  5. QAbstractTableModel for QTreeView?
    By Michiel in forum Qt Programming
    Replies: 5
    Last Post: 15th May 2007, 09:09

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.