Global application menu active in modal dialog box

2019-08-15 06:04发布

I am developing an application in PySide on Windows and Linux (Ubuntu). On Windows everything works as expected. But on Linux, when I open a modal dialog box in the application (even system dialogs, e.g. OpenFileDialog), the global application menu still stays visible and active. What is the meaning of modality then?

I know this is likely a duplicate to this: Application menu is enabled even if a modal dialog box is open

According to it, if I understand well, I would have to keep the reference to the global application menu in each dialog box and disable all the menu actions when the modal dialog box is opened. And enable it when it is closed. Hard to believe this is the only option. It is just stupid overkill...

So is there any other option how to solve it? For example keeping the menu bar below the main window title bar as we have in on Windows... I know it may be not native Linux "feel and look" then but it is much better than having global menu in modal windows.

Or am I missing some simple and obvious solution?

1条回答
Lonely孤独者°
2楼-- · 2019-08-15 07:04

You can actually keep the menu bar right below the window title to avoid this problem by disabling the QMenu.nativeMenuBar property.

Here is an example with PyQt4:

from PyQt4 import QtGui


class MainWindow(QtGui.QMainWindow):

    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)

        menu_bar = self.menuBar()
        # Deactivate global menu bar on Ubuntu
        menu_bar.setNativeMenuBar(False)
        # Add menu for show
        menu_bar.addMenu('Some menu...').addAction('Hit me!')

        # Open modal dialog for test
        self.button = QtGui.QPushButton('Open dialog', self)
        self.setCentralWidget(self.button)
        self.button.clicked.connect(
            lambda: QtGui.QMessageBox.information(self, 'Hello!', "I'm really modal")
            )


if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)

    w = MainWindow()
    w.show()

    sys.exit(app.exec_())
查看更多
登录 后发表回答