Catch the click on the icon of an QListview entry.
-
wrote on 3 Apr 2022, 10:31 last edited by
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.
-
wrote on 3 Apr 2022, 15:24 last edited by
Yes this was it. Using an TableView will to the job.
-
The icon can be retrieved via QAbstractItemModel::data(index, Qt::DecorationRole).
-
wrote on 3 Apr 2022, 11:47 last edited by
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. -
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.Lifetime Qt Championwrote on 3 Apr 2022, 11:57 last edited by Christian Ehrlicher 4 Mar 2022, 11:58@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(). -
wrote on 3 Apr 2022, 12:11 last edited by
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. -
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. -
wrote on 3 Apr 2022, 13:13 last edited by
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. -
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
-
wrote on 3 Apr 2022, 13:36 last edited by
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".
-
wrote on 3 Apr 2022, 15:24 last edited by
Yes this was it. Using an TableView will to the job.
5/10