Unsolved Custom role for a custom model
-
Hi.
I need a custom role for my models model::data(). Are there any or can i create some? -
you can use
Qt::UserRole
and any number above, for exampleQt::UserRole+1
and so on -
Just add a number? Cant i define Qt::UserRoleTwo or something like that?
-
@Maser said:
Just add a number? Cant i define Qt::UserRoleTwo or something like that?
Sure, what you normally do is something like this:
enum MyRoles{ myRole1=Qt::UserRole ,myRole2 ,myRole3 // and so on };
-
You mean it doesnt have to be in the Qt:: namespace?
-
@Maser
Nope, its just a start value.
So internal roles ID is < Qt::UserRole -
How you mean internalrole. I want it passed between classes, so it kinda have to be global.
-
"internal roles" are the roles already predefined by Qt (
Qt::ItemDataRole
).As you see in the API the
data(...)
method treats the role as an int so you're free to specify it as enum, as #define or as magic number - wherever you want. As long as you use values greater or equalQt::UserRole
ot prevent conflicts (but the prefered way is to specify an enum, see @VRonin).Depending on what you want to to with the values associated with your roles you have to extend your view so your roles must be at least so "global" that they are known by the model and by the corresponding view.