Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qmenu aboutToShow do not freeze the program while it is open



  • Hi Guys!
    Sorry again if its a rally lame question.
    I have some QPushbuttons with hover effect (change the background to yellow) and these buttons have Qmenus (button->setMenu(Menu1)).
    When i click one of them, the background turns yellow and perfectly drop down the menu items. Til it is open the background is freez, so i move the cursor above another button, not turn into yellow, but when i click on it (the opened menu is perfectly close and the menubutton turn into the default color) the menu is perfectly open but the menuPushbutton not turn into yellow.

    So what i would like to achive when a menu is opened and i move to another buttonmenu turn into yellow with hover effect, and when i clicked to another the new one turn into yellow and drop the menu as well.

    I see its a pretty bad attempt but i dont have any other solutions :(

    Any advice? wehere is my mistakes? Ty guys!


  • Lifetime Qt Champion

    @Kaguro

    Hi
    Im not sure its possible to make it work. Its about toplevel widgets and we cannot change that for QMenu
    as then it stop working.

    The best work around in my opinion is to use an event filter to make it close if the user moves the mouse outside the menu

    .. somewhere...
        menuSettings = new QMenu(this); // menuSettings must be a member
        menuSettings->addAction("Config CAMS");
        menuSettings->addAction("Alarms");
        menuSettings->addAction("Storage");
        menuSettings->addAction("Plano");
        menuSettings->addAction("Advance");
        menuSettings->addAction("Report");
        ui->pushButton->setMenu(menuSettings);
    
        menuSettings->installEventFilter(this); //  INSTALL event filer
    }
    
    
    bool MainWindow::eventFilter(QObject *obj, QEvent *event)  // the filter
    {
        if (obj == menuSettings && event->type() == QEvent::Leave) {
        if (menuSettings)
          menuSettings->close();
       }
        return false;
    }
    

    alt text


  • Lifetime Qt Champion

    Hi,

    Yellow not being a usual color, is it part of your desktop color scheme ? Custom to your application ? Are you using a stylesheet ?



  • @SGaist the color is not important. The fact that freez the bacground (until i don't choose a menu item) and not activate another hover effect when a qmenu is open is the question.

    Okay sorry for my bad english. So i illustrate with some pictures (the red point is my cursor)
    1: i move with cursor above my button (its okay)
    1.png
    2: i click on it and drop down the menu and i move the cursor hover the menu items (its okay)
    2.png
    3: i dont click, just move hover the "Pre set" button... and its not okay, couse the hover effect is not activated, but i want to activate hover effect (so the button's background not turn into yellow)
    3.png
    4: and when i click on it (the "pre set" button), not turn into yellow, but i want to turn it to yellow. the menu items works well here too.
    4.png


  • Lifetime Qt Champion

    Hi
    You can try using
    https://doc.qt.io/qt-5/qmenu.html#popup
    instead of
    https://doc.qt.io/qt-5/qmenu.html#exec
    to show the dropdown menu.

    The reason for the hover event is not coming for other widgets when the menu is open could be due to the use
    of exec() or that its considered a toplevel window and hence also steals the events.



  • @mrjj Hello!
    I use aboutToShow not exec. because i wanted to copy the menubutton size (for all menuitem).
    But if i tried with the popup the issue is the same :( And the menu items size not correct.


  • Lifetime Qt Champion

    @Kaguro
    Hi
    Ok so even if you use popup to show the dropdown menu, the
    other widgets still dont get the hover event ?

    aboutToShow is just a signal to say menu is about to be shown.
    Its not really related to your current issue.

    I think the main issue is that QMenu takes all events when open.
    Not 100% sure why yet.



  • @mrjj Yes! If i used popup the other widgets still dont get the hover event. Everything else is "freeze".
    Yeah you are right the abouttoshow is not related in this problem.


  • Lifetime Qt Champion

    So if I understand correctly you would like to have your set of buttons behave more or less like a menu bar ?



  • @SGaist Yes! You are right! Like the menubar



  • @mrjj @SGaist So the correct question: How can i create a menu bar with Pushbuttons (and the QPushButtons have menu)?


  • Lifetime Qt Champion

    @Kaguro

    Hi
    Im not sure its possible to make it work. Its about toplevel widgets and we cannot change that for QMenu
    as then it stop working.

    The best work around in my opinion is to use an event filter to make it close if the user moves the mouse outside the menu

    .. somewhere...
        menuSettings = new QMenu(this); // menuSettings must be a member
        menuSettings->addAction("Config CAMS");
        menuSettings->addAction("Alarms");
        menuSettings->addAction("Storage");
        menuSettings->addAction("Plano");
        menuSettings->addAction("Advance");
        menuSettings->addAction("Report");
        ui->pushButton->setMenu(menuSettings);
    
        menuSettings->installEventFilter(this); //  INSTALL event filer
    }
    
    
    bool MainWindow::eventFilter(QObject *obj, QEvent *event)  // the filter
    {
        if (obj == menuSettings && event->type() == QEvent::Leave) {
        if (menuSettings)
          menuSettings->close();
       }
        return false;
    }
    

    alt text



  • @mrjj Oh yeah! This is it and works! :D Many many thanks my hero!


Log in to reply