PDA

View Full Version : Checkboxes in QAbstractITemModel



Valheru
28th November 2007, 18:57
I've gotten some checkboxes displayed in a QTreeView, which has a model that is a subclass of QAbstractItemModel. The checkboxes show up fine for the column I want them to, but I would like to have the checkboxes unly become checked/unchecked when the user clicks on the checkbox itself. At the moment, the checkbox recieves click events when the user clicks in the column/row the checkbox is in. So the user only has to click on a row in column 0 in order for the checkbox to be toggled. how can I get Qt to check if the checkbox itself has been clicked?

jpn
28th November 2007, 19:29
I've gotten some checkboxes displayed in a QTreeView
Are we talking about checkable items or real QCheckBoxes?


but I would like to have the checkboxes unly become checked/unchecked when the user clicks on the checkbox itself.
This is how checkable items work by default. See the attached example.

Valheru
28th November 2007, 20:01
They're checkable items, sorry. The model's data() function returns Qt::IsUserCheckable for column 0 in the model.

I know how checkable items work in a QStandardItemModel, that is in fact exactly what I would like to achieve in my implementation. But that isn't how they are working.

I've attached the source of a simple implementation. You'll need CMake to build it.

wysota
28th November 2007, 20:31
If you do this:

void ModelTestModel::clicked( const QModelIndex& index )
{
if( index.column() == 0 ){
ModelTestModelItem *item = static_cast< ModelTestModelItem* >( index.internalPointer() );

if( item->checkState() == Qt::Checked )
item->setCheckState( Qt::Unchecked );
else
item->setCheckState( Qt::Checked );

_parent->update( index );
}
}
then no wonder a click on the whole area makes the item checked. There is no need for such things. The checkable functionality is provided out of the box by the framework. For you it doesn't work, because you didn't implement setData(). If you do and handle changing the Qt::CheckStateRole, it will work as expected.

Valheru
28th November 2007, 20:49
I'm having trouble getting the setData() function of the QAbstractItemModel to work. Currently it looks like this :


bool ModelTestModel::setData( const QModelIndex &index, const QVariant& value, int role )
{
// qDebug( index );
qDebug( value.toString().toLatin1() );
qDebug( QString::number( role ).toLatin1() );
// if( role != Qt::CheckStateRole )
// return false;

ModelTestModelItem *item = static_cast<ModelTestModelItem*>( index.internalPointer() );

if( item->checkState() == Qt::Checked )
item->setCheckState( Qt::Unchecked );
else
item->setCheckState( Qt::Checked );

// emit dataChanged( index, index );
return true;
}

But the debug lines are not appearing in the console. It seems as though it is never getting called. Why would that be? Doesn't it automatically get called internally by QAbstractItemModel whenever you click on a QModelIndex?

Valheru
28th November 2007, 21:23
nm, for some wierd reason I'd removed the Qt::ItemIsUserCheckable return from data() >_<. Once I'd put that back it was fixed. Thanks for the help.