Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to ignore the Mouse Wheel Event when Combo box is not in Focus ?
Forum Updated to NodeBB v4.3 + New Features

How to ignore the Mouse Wheel Event when Combo box is not in Focus ?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 18.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Macro
    wrote on last edited by
    #1

    Hi.. I have a QComboBox and few QLineEdits, in that currently my Focus is in any of the QLineEdit and i can enter some values in it.. At that time, my Mouse Pointer is in the QComboBox (QComboBox hover state).. Suppose if i scroll my mouse the focus is changing to the QComboBox and the MouseWheel event is taking place.... How can i ignore the Mouse wheel Event when the Combo Box has no focus in it.... ie) The Mouse Wheel Event should be allowed only when the combo Box is clicked and when it has focus.... Plz post your suggestions.....

    Thanks & Regards...

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      This will prevent combo from getting focus on mouse wheel event:

      comboBox->setFocusPolicy( Qt::StrongFocus );
      

      Then you either install an event filter on the combo and filter out wheel event when it has no focus or subclass the QcomboBox class, reimplement wheelEvent(...) and call the original QComboBox::wheelEvent only when it has focus.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Macro
        wrote on last edited by Chris Kawa
        #3

        This will prevent combo from getting focus on mouse wheel event:
        comboBox->setFocusPolicy( Qt::StrongFocus );

        Hi... I tried in this way.. But it doesn't prevent the comboBox from getting focus on mouseWheelEvent...

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          It may not seem it does, but it does. Check the hasFocus() function inside the wheel event with and without it ;) But it's not enough to do what you want.

          Anyway here are both solutions I proposed previously. I checked and they both work fine:

          //custom combo behavior
          class CustomCombo :public QComboBox {
          public:
             using QComboBox::QComboBox;
          
              void wheelEvent(QWheelEvent *e)
              {
                  if(hasFocus())
                      QComboBox::wheelEvent(e);
              }
          };
          
          //and later on in your code
          CustomCombo* combo = new CustomCombo(this);
          combo->setFocusPolicy( Qt::StrongFocus );
          combo->addItems(QStringList() << "1" << "2" << "3" << "4");
          

          or

          //SomeObject can be any QObject, e.g your main window
          bool SomeObject::eventFilter(QObject *obj, QEvent *e)
          {
              if(e->type() == QEvent::Wheel)
              {
                  QComboBox* combo = qobject_cast<QComboBox*>(obj);
                  if(combo && !combo->hasFocus())
                      return true;
              }
          
              return false;
          }
          
          //and then
          QComboBox* combo = new QComboBox(this);
          combo->setFocusPolicy( Qt::StrongFocus );
          combo->installEventFilter( someObjectInstance );
          combo->addItems(QStringList() << "1" << "2" << "3" << "4");
          
          1 Reply Last reply
          6

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved