[Qt-interest] QIcon inside QComboBox....how to get mouse events?

Andre Somers andre at familiesomers.nl
Tue Mar 9 16:06:29 CET 2010


Darryl Campbell wrote:
>
> Hello, I want to include a "remove" icon on entries in my QComboBox, 
> but I am having trouble catching the mouse press event. I've tried to 
> catch it on the combobox, and I've tried reimplemting the QIcon class 
> to catch the mousepress there. No dice. If you use Internet Explorer 
> you can see what I'm talking about in the list of websites in the 
> dropdown location bar. Does anybody know how to do this?
>
QIcon is not a widget, so it doesn't do anything with mouse events. It's 
just a container for an image in different states. Nothing to 
reimplement there.

A QComboBox is basically a QLineEdit with a button next to it. If the 
button is pressed, the combo box pops up a QAbstractItemView (a 
QListView by default) that contains items to choose from. So, if you 
want to modify that list, you should modify that view, not the QComboBox 
itself.

Luckely, you can use your own item view as the popup list using 
QComboBox::setView. You can also set any model you like on that view. 
What I would try, is create a model that not only contains the text 
items, but a second column with an icon for your remove command. Then, 
instead of a QListView as the view for the combo box, you use a 
QTableView (with removed headers and no lines between items) where the 
first column is the normal text, and the second column contains the 
icons. Hook up QAbstractItemView::clicked() to some slot, and if the 
click is in your second column, do the removing.

Something like that should work, I'd say judging on a glance at the 
documentation.

André




More information about the Qt-interest-old mailing list