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 aQt.ItemIsUserCheckable
flag activated somewhere in the definition of yourQTableView
, and if that's the case, deactivate it. You could try to modify theflags
method, for example, forcing every entry not to be checkableAs an additional comment, you should probably modify your
::data
method to take into account the case whereindex
is 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
role
allows 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 theQString
corresponding to your current cell. You could also return aQBrush
for painting the background if your role isQt.BackgroundRole
...While not mandatory, these tests on
role
are still highly encouraged: it makes your code cleaner and easier to maintain.