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;
}
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;
}
To copy to clipboard, switch view to plain text mode
my subclassed QAbstractTableModel looks like this (with added inlines to keep it shorter)
{
Q_OBJECT
enum AMSJobsListModelRoles
{
JobIDRole = Qt::UserRole + 1,
JobStatusRole
};
public:
AMSJobsListModel
(QObject *parent
= 0);
virtual ~AMSJobsListModel(void);
public:
{
if (!parent.isValid())
{
(int)_jobs.size();
}
else
{
return 0;
}
}
{
return 2;
}
{
if (index.row() < 0 || index.row() >= _jobs.size())
if (role == JobIDRole)
return _jobs[index.row()]->getJobID();
else if (role == JobStatusRole)
return _jobs[index.row()]->getJobStatus();
}
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;
};
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;
};
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
Bookmarks