I started to learn Qt, and I would like to implement a table filled with data via QTableView. My problem is, that I don't know how to remove the checkboxes from the cells. It seems like they are put in by default.
However, I read that I had to return a NULL-QVariant, but that's not what I was looking for as I still have data to put in.
That's my code so far:
QVariant MyModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column();
QString daten;
switch (col)
{
case 0:
{
daten = "column 1";
break;
}
case 1:
{
daten = "column 2";
break;
}
case 2:
{
daten = "column 3";
break;
}
case 3:
{
daten = "column 4";
break;
}
}
return daten;
}
Now, as you can see, I want to fill the cell with the QString called "daten". But next to the String there is a Checkbox in every cell.
Does somebody know how to remove the checkbox but still fill the content with "daten"?
Thanks!
The fact that the cells in your QTableView
have some checkbox hint that they were defined as user-checkable. Check whether you don't have a Qt.ItemIsUserCheckable
flag activated somewhere in the definition of your QTableView
, and if that's the case, deactivate it. You could try to modify the flags
method, for example, forcing every entry not to be checkable
As an additional comment, you should probably modify your ::data
method to take into account the case where index
is invalid and to return some value only if the role corresponds to Qt.DisplayRole
. In Python, the syntax would be
if index.isvalid():
if (role == Qt.DisplayRole):
(row, col) = (index.row(), index.column()
return_something_depending_on_col
return QVariant()
return QVariant()
That way, you cover the case of an invalid index, your code would likely crash otherwise.
The test on role
allows you to choose which type of data you want to access. The documentation states for example that:
Each item in the model has a set of data elements associated with it, each with its own role. The roles are used by the view to indicate to the model which type of data it needs. Custom models should return data in these types.
The basic role is Qt.DisplayRole
, where you return the QString
corresponding to your current cell. You could also return a QBrush
for painting the background if your role is Qt.BackgroundRole
...
While not mandatory, these tests on role
are still highly encouraged: it makes your code cleaner and easier to maintain.