PDA

View Full Version : How to make QAbstractItemModel 's data checkable



nifei
4th February 2009, 06:27
Hi,
I want to make some data of QAbstractItemModel to be user-checkable. It seems that could be done via QAbstractItemModel : : flags(index), however, how to change the model's flags?

the QStandardItemModel has the interface setCheckable(bool) to make an item checkable or not, but the QAbstractItemModel does not.

By now, i can get the flags by


bool b = index.flags() & Qt::ItemIsUserCheckabel;


but how to set it?

aamer4yu
4th February 2009, 07:18
You will need to save flags in your own defined role.
Thats how standard item does it. You can have a look at QStandardItem::setFlags code.

nifei
4th February 2009, 07:32
You will need to save flags in your own defined role.
Thats how standard item does it. You can have a look at QStandardItem::setFlags code.

Yes i can save the flags but how to set them to QAbstractitemModel, which has not the similar interface to control the flags?

spirit
4th February 2009, 07:38
when you set data using Qt::CheckStateRole a checkbox will appear in item, but you also need to set flags as aamer4yu said.

nifei
4th February 2009, 07:47
when you set data using Qt::CheckStateRole a checkbox will appear in item, but you also need to set flags as aamer4yu said.

When i set data via Qt::CheckStateRole the item becomes user - checkable automatically. However I have to manage QAbstractItemModel instead of QStandardItemModel, then how to change the flags via index as there is no item in QAbstractItemModel?

spirit
4th February 2009, 07:49
When i set data via Qt::CheckStateRole the item becomes user - checkable automatically.

set it unchecked :)



However I have to manage QAbstractItemModel instead of QStandardItemModel, then how to change the flags via index as there is no item in QAbstractItemModel?

you have to reimplement Qt::ItemFlags QAbstractItemModel::flags ( const QModelIndex & index ) const

nifei
4th February 2009, 07:50
You will need to save flags in your own defined role.
Thats how standard item does it. You can have a look at QStandardItem::setFlags code.


void QStandardItem::setFlags(Qt::ItemFlags flags)
{
setData((int)flags, Qt::UserRole - 1);
}

got it, thanks a lot.

spirit
4th February 2009, 07:58
void QStandardItem::setFlags(Qt::ItemFlags flags)
{
setData((int)flags, Qt::UserRole - 1);
}

got it, thanks a lot.

btw, you can use void QStandardItem::setCheckable ( bool checkable ) insted of setting flags in that case if you use QStandardItem.

DPB1956
22nd August 2011, 21:14
Can you explain how what you posted fixed your problem if you’re still around? I’m having the same problem making QAbstractItemModel 's data checkable.
I can set the checked/unchecked state in data() but I can’t set the checkbox to a checkable state. I added Qt::ItemIsUserCheckable to flags() but this does nothing. How did what you posted:

void QStandardItem::setFlags(Qt::ItemFlags flags)
1. {
2. setData((int)flags, Qt::UserRole - 1);
3. }
fix the problem?

You were told to “save flags in your own defined role”. How does this work and how exactly were you able to do this? I’ve looked at the code for QStandardItem::setFlags() but it includes nothing about roles.

norobro
23rd August 2011, 03:08
I can set the checked/unchecked state in data()
Not sure why you're setting anything in data(). Anywho try something like the following.

In data():
. . .
if(role == Qt::CheckStateRole){
QStandardItem *item = static_cast<QStandardItem*>(index.internalPointer());
return item->checkState();
}
. . .
Then in setData():
. . .
if(role==Qt::CheckStateRole) {
QStandardItem *item = static_cast<QStandardItem*>(index.internalPointer());
if(item->isChecked()) item->setCheckState(Qt::Unchecked);
else item->setCheckState(Qt::Checked);
return true;
}
return QAbstractItemModel::setData(index, value, role);
}
HTH

ChrisW67
23rd August 2011, 05:39
I’m having the same problem making QAbstractItemModel 's data checkable.
I can set the checked/unchecked state in data() but I can’t set the checkbox to a checkable state. I added Qt::ItemIsUserCheckable to flags() but this does nothing.

Does nothing in what way?

QAbstractItemModel doesn't contain any data, it is an abstract interface that you can implement any way you like. When you create your implementation of the that interface you control how it behaves. If you want a view to allow checking of an item from your model then you need to return Qt::ItemIsUserCheckable amongst the flags() for the item (which should be editable). You will also need to do something sane with Qt::CheckStateRole in the implementation of data() and setData().

If you want a function like QStandardItem::setCheckable() to apply check-ability on an item-by-item basis then you need to add the method to your implementation and find somewhere to store the check-ability flags against an item in your model implementation. One solution is to use another role on the item to store/retrieve the check-ability attribute (i.e. like QStandardItemModel does) but you could equally use another structure. How you do it is entirely up to you because only you know what the data being presented through the model looks like.

Whether a view does anything with the checkable state is another question.

DPB1956
23rd August 2011, 08:36
Thanks for responding. After adding setData() the checkbox is now working properly. I was under the impression that setting Qt::ItemIsUserCheckable to flags() would work the same way as setCheckable(true) in QStandardItemModel where the checkbox could be toggled without setting any additional flags.

Thanks for your help.

Anjan@369
1st April 2013, 20:52
I am still unable to set the flag as in my flag function i made it as Qt::ItemIsUserCheckable;
but my got got crashed in data function at item->checkState();
pls help me out how to set or save the flag in which function and how to use it...
thnx in adv.