PDA

View Full Version : TableView and QAbstractTableModel



MattieB
11th December 2013, 16:13
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:



TableView
{
anchors.margins: 12;
anchors.fill: parent;
TableViewColumn
{
role: model.jobID;
title: "Job ID";
width: 120;
}
TableViewColumn
{
role: model.jobStatus;
title: "Job Status";
width: 120;
}
model: operatorMainStateQMLInterface.model;
}


my subclassed QAbstractTableModel looks like this (with added inlines to keep it shorter)


class AMSJobsListModel: public QAbstractTableModel
{
Q_OBJECT

enum AMSJobsListModelRoles
{
JobIDRole = Qt::UserRole + 1,
JobStatusRole
};

public:
AMSJobsListModel(QObject *parent = 0);
virtual ~AMSJobsListModel(void);
public:

virtual int rowCount (const QModelIndex &parent = QModelIndex()) const
{
if (!parent.isValid())
{
(int)_jobs.size();
}
else
{
return 0;
}
}

virtual int columnCount (const QModelIndex &parent = QModelIndex()) const
{
return 2;
}
QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
{
if (index.row() < 0 || index.row() >= _jobs.size())
return QVariant();

if (role == JobIDRole)
return _jobs[index.row()]->getJobID();
else if (role == JobStatusRole)
return _jobs[index.row()]->getJobStatus();
return QVariant();
}

QHash<int, QByteArray> roleNames() const
{
QHash<int, QByteArray> roles;
roles[JobIDRole] = "jobID";
roles[JobStatusRole] = "jobStatus";
return roles;
}

private:
std::vector<boost::shared_ptr<AMSJob> > _jobs;
};


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

MattieB
11th December 2013, 20:57
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