PDA

View Full Version : How to capture a right click in QComboBox



buster
22nd September 2020, 17:37
What is the correct way to capture a right click in a QComboBox with a QCompleter? I want the completer to work as normal, but I also want to allow the user to right click in the combo lineedit and see the whole list of textstrings in the model. I have tried sub-classing both QComboBox and QCoompleter and reimplementing "mousePressEvent", but the method isn't called in either case. Do I need an event filter here? If so, should it be in the sub-classed combo or in the completer? The idea is to capture the right click and then incapacitate the completer. Or perhaps there is a more elegant way to achieve this?

buster
23rd September 2020, 22:29
I just found out that I can capture the muse click when I (left)click on the QComboBox's right hand side down arrow, and this is even better than a right click. So problem solved. However, I couldn't find anything about this in the docs. Perhaps I looked in the wrong place?

Out of courosity: the reason it doesn't work when I click in the combo's lineedit it probably because I then "somehow" need to specify that in the mousePressEvent function?

d_stranz
24th September 2020, 01:20
Out of courosity: the reason it doesn't work when I click in the combo's lineedit it probably because I then "somehow" need to specify that in the mousePressEvent function?

This doesn't make any sense. You don't call the mousePressEvent() handler, Qt's event loop does, in response to something generated deep in the internals of your mouse's device driver. So how can you "somehow" specify anything? The event you get describes something that has already happened, it doesn't provide a way for you to specify what happens sometime in the future or for some widget other than the one clicked..

What is more likely is that the combination of editability settings, completers, and other things you have set on your combo box have resulted in right-click events being processed and eaten so they never reach the level of your code. You can try setting an event filter on both the combo box and the line edit inside it to see if you actually do get those events (event filters are called before the widget itself gets a whack at the event).