How to implement visual effect for cutted item in QAbstractItemView without modifying the original data structures?
-
@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
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 aQStyledItemDelegate
attached 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
PersonItem
for model, it is highly coupled withPerson
.<br>
What's more worse is thatPersonItem
should also maintain the relationship aboutparent
&children
. Only the 3rd case below (i.e. derivePersonItem
fromPerson
), is ok, but to get parent/children ofPersonItem
, it gets them via interface ofPerson
and 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?:
so long as your view can access the appropriate information from the QModelIndex passed to it.
Yes, but
QModelIndex
is not enough, e.g. after cutting an item and before pasting it, a sibling before it is deleted,QModelIndex
would not be enough in this case. -
@jronald
I don't know what you mean. EachQModelIndex
identifies 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_index
has 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. -
@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.