How to implement visual effect for cutted item in QAbstractItemView without modifying the original data structures?
-
@JonB said in How to implement visual effect for cutted item in QAbstractItemView?:
I don't know what you mean.
E.g.
QModelIndex model_indexhas row and column, when a item before it is removed,model_index.row()should be decreased by 1, but it doesn't happen automatically.@jronald
So if you want whatever you need to know to be stored in the original item when it is cut, do so viasetData(), then you don't need to remember/adjust theQModelIndex. You might store the desired "cut" icon either directly insetData(Qt::DecorationRole)or in your ownQt::UserRole + ...and test for that indata(Qt::DecorationRole)'s return value. Obviously whateversetData(role)value you use will need to be cleared out when the cut completes or is cancelled. -
As in file explorer, if you cut a file, the visual effect is that the file becoms grey or so.
ForQAbstractItemView, to implement the visual effect for cutting a item, do I have to add a flag to the item in the model?@jronald
I am thinking that would be one way of doing it, yes. By makingdata(Qt::DecorationRole)return a differently-colored icon while the "cut" is active. The other way would be via aQStyledItemDelegateattached to the view withsetItemDelegate(). See also the icon pictures at https://doc.qt.io/qt-5/qicon.html#making-classes-that-use-qicon. -
@jronald
I am thinking that would be one way of doing it, yes. By makingdata(Qt::DecorationRole)return a differently-colored icon while the "cut" is active. The other way would be via aQStyledItemDelegateattached to the view withsetItemDelegate(). See also the icon pictures at https://doc.qt.io/qt-5/qicon.html#making-classes-that-use-qicon.@JonB said in How to implement visual effect for cutted item in QAbstractItemView?:
The other way would be via a QStyledItemDelegate attached to the view with setItemDelegate().
So a flag in model item is always needed.
The problem is e.g.
an item is like this
class Person { public: int id; std::string name; int age; std::shared_ptr<Person> parent std::shared_ptr<Persons> children; }By adding a flag for cutting status it'll like this:
- Simply adding a flag mixes data for model with the essential data, not good enough
class Person { public: int id; std::string name; int age; std::shared_ptr<Person> parent std::shared_ptr<Persons> children; bool is_cutted; } - With dedicated
PersonItemfor model, it is highly coupled withPerson.<br>
What's more worse is thatPersonItemshould also maintain the relationship aboutparent&children. Only the 3rd case below (i.e. derivePersonItemfromPerson), is ok, but to get parent/children ofPersonItem, it gets them via interface ofPersonand cast them toPersonItem, it works but not perfect.
orclass PersonItem { public: int id; std::string name; int age; std::shared_ptr<Person> parent std::shared_ptr<Persons> children; bool is_cutted; }
orclass PersonItem { public: Person Person; bool is_cutted; }class PersonItem : public Person { public: bool is_cutted? }
Maybe an extra class for saving status's like cut/copy is need, e.g.
std::map<int, Status> item_status;The key is for student id, the value is for status.
But it is obviously less efficient. - Simply adding a flag mixes data for model with the essential data, not good enough
-
@JonB said in How to implement visual effect for cutted item in QAbstractItemView?:
The other way would be via a QStyledItemDelegate attached to the view with setItemDelegate().
So a flag in model item is always needed.
The problem is e.g.
an item is like this
class Person { public: int id; std::string name; int age; std::shared_ptr<Person> parent std::shared_ptr<Persons> children; }By adding a flag for cutting status it'll like this:
- Simply adding a flag mixes data for model with the essential data, not good enough
class Person { public: int id; std::string name; int age; std::shared_ptr<Person> parent std::shared_ptr<Persons> children; bool is_cutted; } - With dedicated
PersonItemfor model, it is highly coupled withPerson.<br>
What's more worse is thatPersonItemshould also maintain the relationship aboutparent&children. Only the 3rd case below (i.e. derivePersonItemfromPerson), is ok, but to get parent/children ofPersonItem, it gets them via interface ofPersonand cast them toPersonItem, it works but not perfect.
orclass PersonItem { public: int id; std::string name; int age; std::shared_ptr<Person> parent std::shared_ptr<Persons> children; bool is_cutted; }
orclass PersonItem { public: Person Person; bool is_cutted; }class PersonItem : public Person { public: bool is_cutted? }
Maybe an extra class for saving status's like cut/copy is need, e.g.
std::map<int, Status> item_status;The key is for student id, the value is for status.
But it is obviously less efficient. - Simply adding a flag mixes data for model with the essential data, not good enough
-
@jronald
That is an implementation detail. A flag in the model would be an obvious choice but there are other ways of achieving this if you prefer, so long as your view can access the appropriate information from theQModelIndexpassed to it.@JonB said in How to implement visual effect for cutted item in QAbstractItemView?:
so long as your view can access the appropriate information from the QModelIndex passed to it.
Yes, but
QModelIndexis not enough, e.g. after cutting an item and before pasting it, a sibling before it is deleted,QModelIndexwould not be enough in this case. -
@JonB said in How to implement visual effect for cutted item in QAbstractItemView?:
so long as your view can access the appropriate information from the QModelIndex passed to it.
Yes, but
QModelIndexis not enough, e.g. after cutting an item and before pasting it, a sibling before it is deleted,QModelIndexwould not be enough in this case. -
@jronald
I don't know what you mean. EachQModelIndexidentifies one item in your model, and that is what is passed to a delegate to render it. That's all you have, and you decide in eitherdata()or an item delegate how you want that item drawn.@JonB said in How to implement visual effect for cutted item in QAbstractItemView?:
I don't know what you mean.
E.g.
QModelIndex model_indexhas row and column, when a item before it is removed,model_index.row()should be decreased by 1, but it doesn't happen automatically. -
@JonB said in How to implement visual effect for cutted item in QAbstractItemView?:
I don't know what you mean.
E.g.
QModelIndex model_indexhas row and column, when a item before it is removed,model_index.row()should be decreased by 1, but it doesn't happen automatically.@jronald
So if you want whatever you need to know to be stored in the original item when it is cut, do so viasetData(), then you don't need to remember/adjust theQModelIndex. You might store the desired "cut" icon either directly insetData(Qt::DecorationRole)or in your ownQt::UserRole + ...and test for that indata(Qt::DecorationRole)'s return value. Obviously whateversetData(role)value you use will need to be cleared out when the cut completes or is cancelled. -
@jronald
So if you want whatever you need to know to be stored in the original item when it is cut, do so viasetData(), then you don't need to remember/adjust theQModelIndex. You might store the desired "cut" icon either directly insetData(Qt::DecorationRole)or in your ownQt::UserRole + ...and test for that indata(Qt::DecorationRole)'s return value. Obviously whateversetData(role)value you use will need to be cleared out when the cut completes or is cancelled.@JonB said in How to implement visual effect for cutted item in QAbstractItemView?:
So if you want whatever you need to know to be stored in the original item when it is cut, do so via setData(), then you don't need to remember/adjust the QModelIndex. You might store the desired "cut" icon either directly in setData(Qt::DecorationRole) or in your own Qt::UserRole + ... and test for that in data(Qt::DecorationRole)'s return value. Obviously whatever setData(role) value you use will need to be cleared out when the cut completes or is cancelled.
I think it is the most efficient way. I'll use it.
Meanwhile the pure original class is also preferred, it can be a parameter to other functions.
It's likely that some convertions have to be there.