简体   繁体   English

PyQt MenuBar Mac OSX雪豹

[英]PyQt MenuBar Mac OSX Snow Leopard

I am attempting to add an item to the application menu-bar of a simple PyQt example. 我试图将一个项目添加到一个简单的PyQt示例的应用程序菜单栏中。 However, the following code does not seem to alter the menu-bar at all. 但是,以下代码似乎根本没有改变菜单栏。 The only item in the menu is "Python". 菜单中唯一的项目是“ Python”。 Below is the bulk of the code, minus imports and instantiation. 下面是大部分代码,减去导入和实例化。

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.resize(250, 150)
        self.setWindowTitle('menubar')
        self.modal = False

        exit = QtGui.QAction( QtGui.QIcon('images/app_icon.png'), 'Exit', self )
        exit.setShortcut('Ctrl+Q')
        exit.setStatusTip('Exit application')
        self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))

        menubar = self.menuBar()
        file = menubar.addMenu('File')
        file.addAction(exit)

I've also tried creating a new QMenuBar and using the setMenuBar() method to manually swap out the menu bar. 我也尝试过创建新的QMenuBar并使用setMenuBar()方法手动换出菜单栏。

Any glaring mistakes in the above snippet? 以上代码段中是否存在明显错误?

I know this question is old but, since I was stuck with the same problem, I found that because I was creating an action to quit the application and this action is reserved on OSX to the Application Menu, the File menu did not appear. 我知道这个问题很旧,但是由于遇到同样的问题,我发现由于我正在创建一个退出应用程序的操作,并且此操作在OSX上保留给了Application菜单,因此File菜单没有出现。 As I created a new action on the same menu, it became available. 当我在同一菜单上创建新动作时,该动作可用。 This worked by using the same approach for other OS's: 这通过对其他操作系统使用相同的方法来实现:

self.menubar = self.menuBar()

This was created inside a QMainWindow object. 这是在QMainWindow对象内创建的。

Hope this helps anyone! 希望这对任何人有帮助!

When using PyQt on a mac, the system will intercept certain commands contain the word 'Quit' or 'Exit' and remove them from your menubar because they exist elsewhere. 在Mac上使用PyQt时,系统会拦截某些包含单词“ Quit”或“ Exit”的命令,并将它们从菜单栏中删除,因为它们存在于其他位置。 if a menubar header has no items, it will not display, making it appear as if you haven't modified the menubar. 如果菜单栏标题没有项目,则不会显示它,使其看起来好像您尚未修改菜单栏。

#exit = QtGui.QAction( 'Exit', self ) #this fails on my system
exit = QtGui.QAction( 'SomethingElse', self ) #this displays on my system
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exit)

Also, calling raise_() doesn't change the menubar on my mac. 另外,调用raise_()不会更改Mac上的菜单栏。 I have to manually select the window (by clicking else where then reclicking), if i use raise_(), to get the correct menubar to show for my pyqt app. 如果我使用raise_(),则必须手动选择窗口(通过单击其他位置然后重新单击),以获取正确的菜单栏以显示给我的pyqt应用程序。

Also remember that mac menubars are displayed in the system menubar not in the window like on a Windows or Linux machine. 还要记住,mac菜单栏显示在系统菜单栏中,而不是在Windows或Linux计算机上的窗口中。 This leads us to the other solution, as suggested by Levi501 and Swdev. 正如Levi501和Swdev所建议的那样,这引出了另一种解决方案。 That is to use a non-native menu that appears in the window like so: 那就是使用出现在窗口中的非本地菜单,如下所示:

menubar = self.menuBar()
menubar.setNativeMenuBar(False)

As someone who uses windows and linux alot, this makes alot more sense for my projects. 作为大量使用Windows和Linux的人,这对我的项目来说意义更大。

I found the 'Exit' information here: http://python.6.x6.nabble.com/addAction-to-menubar-td1916296.html 我在这里找到“退出”信息: http : //python.6.x6.nabble.com/addAction-to-menubar-td1916296.html

I don't have PyQt installed on this machine to test this out, but I think on a Mac the QMainWindow.menuBar() function does not return the application wide menu bar. 我没有在此计算机上安装PyQt进行测试,但我认为在Mac上, QMainWindow.menuBar()函数不会返回应用程序范围的菜单栏。

You might try creating a menubar like: 您可以尝试创建菜单栏,例如:

menubar = QtGui.MenuBar()

I'm basing this on the docs for the QMainWindow.menuBar() function here: 我将其基于QMainWindow.menuBar()函数的文档放在这里:

http://doc.qt.io/qt-4.8/qmainwindow.html#menuBar http://doc.qt.io/qt-4.8/qmainwindow.html#menuBar

You might also check out the section labeled QMenuBar on Mac OS X on this page: 您也可以在此页面签出Mac OS X上标有QMenuBar的部分:

http://doc.qt.io/qt-4.8/qmenubar.html#details http://doc.qt.io/qt-4.8/qmenubar.html#details

Hope that helps! 希望有帮助!

Correct. 正确。 On MAC OS we need to use menubar like this: 在MAC OS上,我们需要使用如下菜单栏:

self.menubar = QtGui.QMenuBar() 

And not like this: 而且不是这样的:

self.menubar = QtGui.QMenuBar(MainWindow) 

(without the MainWindow parameter) (不带MainWindow参数)

I suggest the best solution is using QTDesiner to build the UI layout then using pyside-uic tool to convert to a Python class on different platform. 我建议最好的解决方案是使用QTDesiner构建UI布局,然后使用pyside-uic工具转换为不同平台上的Python类。 When I used the UI layout class compiled on Windows in MAC I got this issue. 当我使用Windows在MAC中编译的UI布局类时,出现了此问题。 Solve this issue by simply recompiling the UI layout XML on MAC with the command pyside-uic AppMain.ui -o ui_AppMain.pyp 通过使用pyside-uic AppMain.ui -o ui_AppMain.pyp命令在MAC上重新编译UI布局XML即可解决此问题

After I compare the compiled UI layout class between MAC and Windows the only difference is that on Max OS X new QMenuBar object is created without MainWindow parameter. 在比较了MAC和Windows之间的已编译UI布局类之后,唯一的区别是在Max OS X上创建了没有MainWindow参数的新QMenuBar对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM