Global application menu active in modal dialog box (on Linux)

佐手、 提交于 2019-12-11 11:09:08

问题


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:


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_())


来源:https://stackoverflow.com/questions/32035662/global-application-menu-active-in-modal-dialog-box-on-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!