Clicking on QMacToolBar does not close open QMenus
-
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 <QMacToolBar> #include <QMacToolBarItem> #include <QStackedLayout> #include <QtWidgets> 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; }; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { auto toolBar = new QMacToolBar( this ); auto centralWidget = new MyWidget(this); setCentralWidget( centralWidget ); auto firstItem = toolBar->addItem( QIcon( QPixmap( "/Users/Stack1.png" ) ), "Stack1" ); firstItem->setSelectable( true ); connect ( firstItem, &QMacToolBarItem::activated, [ centralWidget ] { centralWidget->SwitchToTab( 0 ); } ); auto secondItem = toolBar->addItem( QIcon( QPixmap( "/Users/Stack2.png" ) ), "Stack2" ); secondItem->setSelectable( true ); connect ( secondItem, &QMacToolBarItem::activated, [ centralWidget ] { centralWidget->SwitchToTab( 1 ); } ); window()->winId(); toolBar->attachToWindow( window()->windowHandle() ); }
Qt 5.14
MacOs Mojave 10.14.6 -
Hi,
What version of Qt ?
What version of macOS ? -
I can reproduce the same issue on High Sierra, you should go to the bug report system and see if there's something related.
-
@SGaist Thank you.
I couldn't find any similar issue reported on QMacToolbar usage.
So, I have raised on in the board: QTBUG-84022 -
Thanks, just one thing, your code as is cannot be compiled. It would be nice to fix it and add a main function so that that the developer taking a look at it can easily compile it.
-
1/7