PDA

View Full Version : Implementing TableView with one editable CheckBox



scarleton
1st July 2010, 02:07
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

aamer4yu
1st July 2010, 05:45
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.

scarleton
2nd July 2010, 02:02
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

scarleton
2nd July 2010, 04:00
Ok, I found this other thread (http://www.qtcentre.org/threads/18675-boolean-field-checkbox-QTableView) 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:

if(index.column() == _isInSlideShowIdx)
{
if (role == Qt::CheckStateRole)
return QSqlQueryModel::data(index, Qt::DisplayRole).toBool() ? Qt::Checked : Qt::Unchecked;
else if( role == Qt::DisplayRole)
return QVariant();
}

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?

landstreicher
7th July 2010, 15:27
Hi scarleton,

perhaps this will help


ui->tableWidget->setEditTriggers(QAbstractItemView::SelectedClicked );

kakukogou
30th July 2010, 11:42
Hello,

Try void QAbstractItemView::openPersistentEditor ( const QModelIndex & index ) !

I think this is what you want !

Kaku