how to make QToolButton auto pop-up menu ?



  • @class QAutoShowToolBtn:public QToolButton
    {
    public:
    explicit QAutoShowToolBtn(QWidget * parent=0):QToolButton(parent)
    {
    //if()
    }
    protected://override
    virtual void enterEvent( QEvent * e )
    {
    if(menu())
    showMenu();
    QToolButton::enterEvent(e);
    }
    };@
    I override the enterEvent(..) it can pops up the menu but the difficult part is how Can i make the menu close when I mouse move to other QToolButton.

    I have tried 2 ways:
    (1)override the leaveEvent( QEvent * e ) inside it I close the menu but found it's a mistake since I even cann't selelct the item in the menu.(will cause the menu prematurely close )
    (2) @ virtual void enterEvent( QEvent * e )
    {
    if(menu())
    click();
    QToolButton::enterEvent(e);
    }@
    I used click() slot to simulate the click-behavior by the user and hope it will close the previous pop up menu of the other button.
    But also failed. I don't know why the click() slot isn't consistent with the real mouse click ,we all know mouse click outside the pop up menu will close it normally.
    consequently,when I move mouse hover over 2 buttons ,both menus will appear together.

    any advice? thanks in advance.



  • Why not emit a signal inside the enter event say "emit popupMenu(e->globalPos())"
    Then in your mainwindow you have a slot like this

    MyMainWindow::showToolbarPopupSlot(QPoint &p)
    {
    myPopupMenu->popup(p);
    }

    I do this for table views anyway. Hope it is of some help



  • Maybe I don't get it, but why "QToolButton::setMenu":http://qt-project.org/doc/qt-4.8/qtoolbutton.html#setMenu is not sufficient ?



  • Because your redefining the behavior to show on "mouseEnter", if you have your menu as defined by QToolButton cannot you just do

    @virtual void enterEvent( QEvent * e )
    {
    if(menu())
    menu()->popup(e->pos // or e->globalPos() ?)
    }@



  • Ok I see. Install an event filter on the menu and in the eventFilter of your class if object is the menu and event is QEvent::Leave close the menu



  • That's a good problem and you idea for the eventFilter sounds sane but does not Menu have a leaveEvent() method, but that means createing your own MyMenuClass from QMenu.

    How ever you get it to work, please post back, as in the future I may want such behavior in an application. Maybe others too



  • QMenu is a QWidget, so it has leave event



  • bq. Ok I see. Install an event filter on the menu and in the eventFilter of your class if object is the menu and event is QEvent::Leave close the menu

    I have thought this method, but it's not viable!
    because while I hover on 1st QToolbutton ,then Associated menu pops up. but while I don't move the mouse to enter the menu to select 1 item but I move to the other QToolButton. then 2 menus pop up simultaneously.(more worse is the previous one is not closed normally)



  • [quote author="DBoosalis" date="1403179628"]Because your redefining the behavior to show on "mouseEnter", if you have your menu as defined by QToolButton cannot you just do

    @virtual void enterEvent( QEvent * e )
    {
    if(menu())
    menu()->popup(e->pos // or e->globalPos() ?)
    }@[/quote]

    thanks any way.but you may don't get what I have asked.The kay point is not about the pops the menu but how can I control the menu automatic close .

    Even I hover more than 1 QToolbuttons consecutively but I don't move the mouse enters the any menus pop up before.



  • [quote author="jzq740176597" date="1403227276"]bq.
    I have thought this method, but it's not viable! [/quote]

    Sorry for delay. Why is not viable ? I used this type of automatically show/hide menu and is viable.

    [quote author="jzq740176597" date="1403227276"]bq.
    then 2 menus pop up simultaneously.(more worse is the previous one is not closed normally)[/quote]

    That's why I said to install an event filter on the menu. If mouse leave the menu, hide it.



  • @jzq740176597 Did you manage to get it working? I'm facing the same problem here...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.