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 make QToolButton auto pop-up menu ?

how to make QToolButton auto pop-up menu ?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 4.3k Views
  • 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.
  • J Offline
    J Offline
    jzq740176597
    wrote on last edited by
    #1

    @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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DBoosalis
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cincirin
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DBoosalis
          wrote on last edited by
          #4

          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() ?)
          }@

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cincirin
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DBoosalis
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                cincirin
                wrote on last edited by
                #7

                QMenu is a QWidget, so it has leave event

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jzq740176597
                  wrote on last edited by
                  #8

                  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)

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jzq740176597
                    wrote on last edited by
                    #9

                    [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.

                    N 1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      cincirin
                      wrote on last edited by
                      #10

                      [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.

                      1 Reply Last reply
                      0
                      • J jzq740176597

                        [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.

                        N Offline
                        N Offline
                        Nuno Martins
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0

                        • Login

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