Catch the click on the icon of an QListview entry.
-
Hi,
I have an QListView, that works like expected.
Before every entry an icon is shown. But how can I catch the click on Icon?
With the clicked(const QModelIndex &index) signal of the view, I only get the line in the view/model.
The itemDelegateForIndex call, don't deliver me an pointer to the QIcon of the line.
So how can I do it?Thanks for any ideas.
-
Yes this was it. Using an TableView will to the job.
-
The icon can be retrieved via QAbstractItemModel::data(index, Qt::DecorationRole).
-
This will be possible, but the QIcon class hast no function for an click state of it.
The QIcon is set when the decoration role is requested in the data model. -
Christian Ehrlicher Lifetime Qt Championreplied to Hans Hansen on last edited by Christian Ehrlicher
@Hans-Hansen said in Catch the click on the icon of an QListview entry.:
This will be possible, but the QIcon class hast no function for an click state of it.
What's wrong with the clicked() signal?
If you want a special clicked on the icon (for whatever reason) you have to calculate t by yourself e.g. in a slot connected to clicked(). -
I get only the index of model, but not if the signal was triggered by an click on the text part of the line or the QIcon.
I think I need something like an rectangle and then check if the mouse was in it. -
@Hans-Hansen said in Catch the click on the icon of an QListview entry.:
I think I need something like an rectangle and then check if the mouse was in it.
Correct. The mouse position is known, the rest can come from a customized QStyledItemDelegate where you can get the corresponding rects via QStyle::subElementRect().
Still don't know why it's important to know where in the cell the click occurred. -
The QStyledItemDelegate is accessible via itemDelegateForIndex from the view. But I can't see an subElement function for it:
https://doc.qt.io/qt-6/qstyleditemdelegate-members.html.
My idea is, that I have left of every text line in the view an cross icon. An when an click on the icon was done, then the line is removed form the model. The remove part itself is easy, only the code part for detecting if the click was on the text or the icon makes the problem. -
@Hans-Hansen said in Catch the click on the icon of an QListview entry.:
But I can't see an subElement function for it:
Because there is non, read my post.
My idea is, that I have left of every text line in the view an cross icon.
Simply use a separate column
-
I will try it with in Tableview. One row for the icon an one for the text, then clicked(const QModelIndex &index) signal should give me automatic the "possiton".
-
Yes this was it. Using an TableView will to the job.