0

I am using Qt Creator and C++ for my code. I have a checkable combobox for multiselection. I want to select the unchecked checkbox using mouse Middlebuton from popup. I tried to override the mousePressEvent but didn't work. Below is the snippet of my code:

void GCheckEditCombobox::mousePressEvent(QMouseEvent *event)
{
     if (event->button() == Qt::MiddleButton && currentItem())
     {
        // doe the stuff you have to 
     }
     else
     {
        QComboBox::mousePressEvent(event);
     }
} 
  • I just made a [mcve]. I can attest you that there is nothing wrong with the middle mouse button in a combo box (at least, on my side). Though, I noticed that the combo box doesn't process mouse events when the drop down menu is open. (I assume that mouse events are redirected to that drop down menu which is surely a separate widget.) – Scheff's Cat May 19 '20 at 13:24
  • how can I apply mouse event for drop-down menu, I have edited my post – neha mundokar May 20 '20 at 05:44
  • Sorry, can you, please, clarify what does _I want to select the unchecked checkbox_ mean? (Which checkbox?) – Scheff's Cat May 20 '20 at 05:45
  • I scrolled a bit through the doc. and the sources on [woboq.org](https://code.woboq.org/qt5/qtbase/src/widgets/widgets/qcombobox_p.h.html). It seems that the "drop down menu" is an implementation detail you cannot rely on. It may even use native platform widgets in certain cases. My idea was to try an [Event Filter](https://doc.qt.io/qt-5/eventsandfilters.html#event-filters) but I'm not sure anymore whether this is a good idea i.e. whether this would work reliably. – Scheff's Cat May 20 '20 at 05:56
  • I would recommend you to solve your intention by design: Instead of trying to support the middle mouse button (which might become difficult and unreliable) use something else for your purpose - like e.g. another button or checkbox which is placed next to your combobox. `QPushButton` might be a not so lucky choice (due to its default style). You may fiddle with the style sheet but I often use `QToolButton` instead. (It seems that a tool button can be used without a parent `QToolBar` and it's default style appears IMHO much better for such cases.) – Scheff's Cat May 20 '20 at 06:01
  • Another alternative could be to build up your own combo box from pieces like `QTextEdit`, `QPushButton` or `QToolButton`, `QListView` etc. However, this may result in more effort than it seems beforehand. So, before becoming depressed finally - think about how much the middle mouse button feature is worth to you. ;-) – Scheff's Cat May 20 '20 at 06:04
  • Your solution really worked, thankyou - Scheff – neha mundokar May 20 '20 at 08:39

0 Answers0