PDA

View Full Version : Which QWidget is used to display the QVariant QAbstractTableModel::data(...) returns?



hml
3rd April 2010, 10:54
Hello,

I have a model, a view and a delegate, each sub classing QTableView QAbstractTableModel and QStyledItemDelegate.

My model's data(...) function returns QString for 2 columns and a QStringList for the 3rd column.

Which class chooses the QWidget to display the cell based on the QVariant returned by data(...)?

Regards,

hml
3rd April 2010, 11:31
I realize now the only case handled is when data(...) returns a QString

wysota
3rd April 2010, 11:47
I realize now the only case handled is when data(...) returns a QString

No, that's not true. Integers and dates (and anything that can be converted to a string) are handled nicely as well. You can't show a list with the default delegate, though, that's true.

hml
3rd April 2010, 14:22
Can I register a "handler" for lists to the default delegate?
I am using a custom delegate for editing anyways. But I want to change the DisplayRole widget....
Is this doable?

My work around has been to concatenate the list inside a QString. However, I get that QString in my delegate's setEditorData() as well, and now have to parse the QString to separate the items an use the individual items to set data in the editor widget.

If I could pass around from model's data() to delegate's setEditorData() the QStringList as the QVariant, it would be much nicer.

I can also write a different data() function of my specific model and have my delegate's setEditorData() not call the model base class data() but the concrete model's other data() function.

Thank you,

wysota
3rd April 2010, 15:34
Can I register a "handler" for lists to the default delegate?
Yes, of course.

But I want to change the DisplayRole widget....
The delegate doesn't use any widgets for DisplayRole. Only the editor is a widget.


If I could pass around from model's data() to delegate's setEditorData() the QStringList as the QVariant, it would be much nicer.
What prevents you from doing that?


I can also write a different data() function of my specific model and have my delegate's setEditorData() not call the model base class data() but the concrete model's other data() function.
But why? You can return any data you want from data() as long as it fits into QVariant. Also note you are not limited to the roles handled by the default delegate. Your delegate (and everything else involved into the architecture) can use custom roles to do its work.

hml
3rd April 2010, 15:56
Ok,

so you are saying reuse the data() function of the model, and instead of having my delegate's setEditorData() call the model's data() with Qt::EditRole, use the user-specific role as the "role" argument and then change data() to return QStringList or actually QMap in my case.

I will then just ignore the EditRole in the model's data() because I assume the only who ever calls the data( ... EditRole) is my delegate...

thanks,

ps: perhaps you looked at my other post regarding the editor widget not hiding the cell displayed underneath,

regards,

wysota
5th April 2010, 19:17
so you are saying reuse the data() function of the model,
Not "reuse". Just "use".


and instead of having my delegate's setEditorData() call the model's data() with Qt::EditRole, use the user-specific role as the "role" argument

and then change data() to return QStringList or actually QMap in my case.
Yes, for that specific custom role.


I will then just ignore the EditRole in the model's data() because I assume the only who ever calls the data( ... EditRole) is my delegate...
I don't know if the only one but probably so. Of course if you are the only one using EditRole then you can return your map from that.


ps: perhaps you looked at my other post regarding the editor widget not hiding the cell displayed underneath,
No, but the behaviour is perfectly normal and documented.

hml
5th April 2010, 21:19
Thank you for your help,

rds,