Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-84022

Clicking on QMacToolBar does not close open QMenus

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3: Somewhat important
    • None
    • 5.14.1
    • Extras: Mac, GUI: Menus
    • None
    • MacOs Mojave 10.14.6
    • macOS

    Description

      I have a MainWindow that has a ToolBar(QMacToolBar) and a central widget with a QStackedLayout. Based on the Tool selected in ToolBar the corresponding widget in QStackedLayout should be shown.
      Some of the widgets in the Stacked Layout can open a QMenu(which opens up as a popup). If I click any where in the window(except on any of the ToolItem) the popup closes correctly.

      How can I make the popup close even when I click on the ToolItems?
      In example below there is only two tabs, but in my application there can be multiple tabs which may or may not have a menu popup.

      Code that has been reduced to relevant parts:

      #include <QApplication>
      #include <QMacToolBar>
      #include <QMacToolBarItem>
      #include <QStackedLayout>
      #include <QtWidgets>
      
      // A Widget with a stacked layout containing
      //  1) a widget coloured in cyan
      //  2) a widget with a pushbutton that opens a menu on click.
      class MyWidget : public QWidget
      {
      public:
          MyWidget( QWidget * inParent ) : QWidget( inParent )
          {
              mStackLayout = new QStackedLayout();
      
              // first Stack Entry
              auto firstWidget = new QWidget( this );
              firstWidget->setFixedSize( QSize{ 200, 100 } );
              firstWidget->setStyleSheet( "background-color:cyan" );
      
              // second Stack Entry
              auto secondWidget = new QPushButton( "Actions", this );
              auto menu = new QMenu( secondWidget );
              menu->addAction( "Action1" );
              menu->addAction( "Action2" );
              secondWidget->setMenu( menu );        mStackLayout->addWidget( firstWidget );
              mStackLayout->addWidget( secondWidget );
          }
      
          void SwitchToTab( int index )
          {
              mStackLayout->setCurrentIndex( index );
          }
      private:
          QStackedLayout * mStackLayout;
      };
      
      // A MainWindow that has a QMacToolBar that controls the widget being
      // visible in MyWidget
      class MainWindow : public QMainWindow
      {
      public:
          MainWindow(QWidget *parent = nullptr)
              : QMainWindow(parent)
          {
              auto toolBar = new QMacToolBar( this );
              auto centralWidget = new MyWidget(this);
              setCentralWidget( centralWidget );
              auto firstItem = toolBar->addItem( QIcon( QPixmap( "/Users/Stack1.png" ) ), "Stack1" ); // image missing, but that shouldn't matter
              firstItem->setSelectable( true );
              connect
              (
                  firstItem,
                  &QMacToolBarItem::activated,
                  [  centralWidget ]
                  {
                      centralWidget->SwitchToTab( 0 );
                  }
              );
              auto secondItem = toolBar->addItem( QIcon( QPixmap( "/Users/Stack2.png" ) ), "Stack2" ); // image missing, but that shouldn't matter
              secondItem->setSelectable( true );
              connect
              (
                  secondItem,
                  &QMacToolBarItem::activated,
                  [  centralWidget ]
                  {
                      centralWidget->SwitchToTab( 1 );
                  }
              );
              window()->winId();
              toolBar->attachToWindow( window()->windowHandle() );
          }
      };
      
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
      }
      
      

      Attachments

        1. FirstTab.png
          16 kB
          Sajas K K
        2. SecondTab.png
          22 kB
          Sajas K K
        3. Switching to First.png
          41 kB
          Sajas K K
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            tpochep Timur Pocheptsov
            Sajas01 Sajas K K
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes