Implementing TableView with one editable CheckBox
I have a QTableView that needs the first column to display a QCheckBox. Since the only thing in the QTableView which is editable is the QCheckBox, the user should be able to change the state of one of the QCheckBoxes with a single click, not two clicks (1st to activate the editor and the second to change the state).
I have the QSqlQueryModel setup to allow the one column to be editable, I have also created a QStyledItemDelegate which will display the QCheckBox editor on the far left of the cell when the cell is double clicked, but when it is not active, it displays the underlying value of zero (0) or one (1).
Q: How do I make the table ALWAYS show a CheckBox for this column, not the number zero (0) or one (1)?
Q: How do I set up the table so that a single click will activate and check the QCheckBox?
Sam
Re: Implementing TableView with one editable CheckBox
You dont need an editor for that. Basically your model should provide data for Qt::CheckStateRole.
Also you can refer QTreeWidgetItem::setCheckState how the checkbox it is implemented.
Re: Implementing TableView with one editable CheckBox
I am missing something here. The best I can figure is in the data() method of the model, check to see if the column is the one that should be a check box, if so and if the role is Qt::CheckStateRole, return either Qt::Checked or Qt::Unchecked.
I did this and it is displaying a checkbox, but it is still to the far left and the original value (zero or one) is also being displayed. When I click on the cell, the number is editable.
What am I missing?
Sam
Re: Implementing TableView with one editable CheckBox
Ok, I found this other thread which enlightened me to enhancing the flag method, too. Since my model extends the QSqlQueryModel, here is what is being returned for the check box column:
Qt::ItemIsEditable | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled
I also tweaked what the data method was returning:
Code:
if(index.column() == _isInSlideShowIdx)
{
if (role == Qt::CheckStateRole)
return QSqlQueryModel::data(index, Qt
::DisplayRole).
toBool() ? Qt
::Checked : Qt
::Unchecked;
else if( role == Qt::DisplayRole)
}
But I still have issues:
- The check box is always on the left
- When I click on the check box cell, it makes the space to the right of the check box an editbox control
- When I added a delegate to create a check box editor, I had to still double click on the space to the right to make the editor active, and the editor is offset to the left by a pixel or two.
Any thoughts on how to fix this?
Re: Implementing TableView with one editable CheckBox
Hi scarleton,
perhaps this will help
Re: Implementing TableView with one editable CheckBox
Hello,
Try void QAbstractItemView::openPersistentEditor ( const QModelIndex & index ) !
I think this is what you want !
Kaku