I want 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.
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.
How do I remove the checkbox, but still fill the content with daten?
The fact that the cells in your
QTableViewhave some checkbox hint that they were defined as user-checkable. Check whether you don’t have aQt.ItemIsUserCheckableflag activated somewhere in the definition of yourQTableView, and if that’s the case, deactivate it. You could try to modify theflagsmethod, for example, forcing every entry not to be checkableAs an additional comment, you should probably modify your
::datamethod to take into account the case whereindexis invalid and to return some value only if the role corresponds toQt.DisplayRole. In Python, the syntax would beThat way, you cover the case of an invalid index, your code would likely crash otherwise.
The test on
roleallows you to choose which type of data you want to access. The documentation states for example that:The basic role is
Qt.DisplayRole, where you return theQStringcorresponding to your current cell. You could also return aQBrushfor painting the background if your role isQt.BackgroundRole…While not mandatory, these tests on
roleare still highly encouraged: it makes your code cleaner and easier to maintain.