We are using a QTableView
with Qt 4.6.3, and need a column that only has a checkbox in each cell. We're using a custom subclass of QAbstractTableModel
as the model for the QTableView
. Right now, we have a checkbox by setting the Qt::ItemIsUserCheckable
flag. But we can't figure out how to get rid of the blank textbox next to the checkbox!
How can we make the column only have a checkbox, nothing else?
Note: The answer of Dave works also for Python using PySide or PyQt4. I translated it, and it works great. Additionally, I added the functionality that the checkbox does not accept user input and is shown in a ReadOnly state if the cell is not editable. Thanks Dave for your code!
Here is a solution. For this to work properly, your column should not have the
Qt::ItemIsEditable
orQt::ItemIsUserCheckable
flags set. This reads the boolean values fromQt::DisplayRole
and callssetData()
withQt::EditRole
(i.e. notQt::CheckStateRole
.)