I'm looking at the Data Visualization Tool Tutorial on the QT website where they have an example of creating a menu bar inside a QMainWindow:
self.menu = self.menuBar()
self.file_menu = self.menu.addMenu("File")
This doesn't work for me on OSX 10.13.6. I've also tried using QMenuBar to create my own menu bar rather than using the default one that comes with a QMainWindow:
menu_bar = QMenuBar()
menu_bar.addMenu('File')
self.setMenuBar(menu_bar)
This also has no effect. I never see the "File" option in the menu bar of my app. I just get a generic menu bar with a single "python" option.
I believe it is working, but "File" will only appear once you add actions to the file menu.
menu = self.menuBar()
file = menu.addMenu('File')
file.addAction(QAction('Open...', self))
Related
I am making an application where I want to create a dropdown menu if anyone clicks on the Qaction.
My code
self.navtb = QToolBar("Navigation")
self.navtb.setIconSize(QSize(25, 25))
self.navtb.setMovable(False)
self.addToolBar(self.navtb)
option_btn = QAction(QIcon(os.path.join('images', 'options.png')), "Option", self)
self.navtb.addAction(option_btn)
A QAction can have an associated menu which you can simply set with setMenu(). This menu will pop up (drop down) on click+hold.
Now all you need is to set your button to directly go for the menu on click by altering its popup mode. Note that while the wording pop up is used, the menu will be a proper drop-down menu in a toolbar scenario.
In your example it would roughly translate to:
option_btn.setMenu(...)
self.navtb.widgetForAction(option_btn).setPopupMode(QToolButton.InstantPopup)
For reference, this is how I do it in my code in C++:
// initialize compute menu and let button display menu without holding mouse
actionComputeDisplay->setMenu(new QMenu(widget));
auto btn = qobject_cast<QToolButton*>(toolBar->widgetForAction(actionComputeDisplay));
btn->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup);
How to add an 'about' button to the menu bar of your main window - which when clicked directly opens a dialog with some about text - using PyQT?
Or is that impossible?
Having had a look at the documentation/question and answers online having to do with the menu bar, I get the impression that the QMenuBar only supports triggering events via 'QAction's via menu drop-downs. However I dont want a drop-down for the about button but rather would like it to trigger some showAboutDialog method.
If you have any ideas/links please let me know.
You can add a QAction object directly to the menubar of your MainWindow. Use the QMenuBar.addAction() method for this:
class YourMainWindow(QMainWindow):
def __init__(self):
super().__init__()
menu = QMenuBar()
menu.addAction(show_about_dialog_action)
self.setMenuBar(menu)
I am trying to create a menu with PySide. This is the code
def _createMenus(self):
self._menuBar = QtGui.QMenuBar()
self.setMenuBar(self._menuBar)
self._helpMenu = self._menuBar.addMenu("Help")
self._aboutAction = QtGui.QAction("About", self, statusTip="About", triggered=self._about)
self._helpMenu.addAction(self._aboutAction)
This code does not show anything in the menu bar, except the "python" entry.
Weird enough, if I change "About" in "Aout" it displays the menu. Is there some platform dependent weirdness I should be aware of ?
The menu items on Mac OS X can be moved automatically by Qt to the system-wide menubar depending on the action's text and/or menuRole property (see QMenuBar on Mac OS X for details).
So your "About" menu item should be the first item in the application menu.
Using wxPython, I created a taskbar icon and menu.
Everything works fine (in Windows at least) upon right-click of the icon: i.e., the menu is displayed, and automatically hidden when you click somewhere else, like on Windows' taskbar.
Now I do want to have the menu appear when the icon is left-clicked as well.
So I inserted a Bind() to a left-click in the Frame class wrapper, calling the CreatePopupMenu() of the taskbar icon:
import wx
class BibTaskBarIcon(wx.TaskBarIcon):
def __init__(self, frame):
wx.TaskBarIcon.__init__(self)
self.frame = frame
icon = wx.Icon('test_icon.ico', wx.BITMAP_TYPE_ICO)
self.SetIcon(icon, "title")
def CreatePopupMenu(self):
self.menu = wx.Menu()
self.menu.Append(wx.NewId(), "dummy menu ")
self.menu.Append(wx.NewId(), "dummy menu 2")
return self.menu
class TaskBarFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, style=wx.FRAME_NO_TASKBAR)
...
self.tbicon = BibTaskBarIcon(self)
wx.EVT_TASKBAR_LEFT_UP(self.tbicon, self.OnTaskBarLeftClick)
...
def OnTaskBarLeftClick(self, evt):
self.PopupMenu(self.tbicon.CreatePopupMenu())
...
def main(argv=None):
app = wx.App(False)
TaskBarFrame(None, "testing frame")
app.MainLoop()
This works fine, except that the menu does not disappear automatically when you click somewhere else on your screen. In fact, left-clicking multiple times on the icon creates multiple menus. The only way to hide the menu(s) is to click on one of its items (which you don't always want). I've looked at the available methods of TaskbarIcon, but I failed to be clear about which one to use to hide the menu (.Destroy() didn't work). Moreover, I don't know which event to bind it to (there is a EVT_SET_FOCUS, but I couldn't find any EVT_LOOSE_FOCUS or similar).
So, how to hide the menu upon losing focus?
EDIT: I've inserted a bit more code, to make it more clear
Ah, I've discovered what went wrong. In the statement
self.PopupMenu(self.tbicon.CreatePopupMenu())
I had bound the popup menu to the frame, instead of to the taskbar icon.
By changing it to:
self.tbicon.PopupMenu(self.tbicon.CreatePopupMenu())
all is working well now.
Thanks for all remarks
I think the problem here is that the PopupMenu is usually used in a program's context, not a little icon in the system tray. What that means is that in a normal frame, the popup menu would detect the click the you clicked off of it. Here, you are clicking outside of the wxPython program. Also, the PopupMenu is usually used with EVT_CONTEXT_MENU, not this taskbar event.
You can try wx.EVT_KILL_FOCUS and see if that works since it should theoretically fire when you click off the menu. Or you could ask on the official wxPython forum here: http://groups.google.com/group/wxpython-users/topics
Mike Driscoll
Blog: http://blog.pythonlibrary.org
I am attempting to add an item to the application menu-bar of a simple PyQt example. However, the following code does not seem to alter the menu-bar at all. The only item in the menu is "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.
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. 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.
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. 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. 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.
Also remember that mac menubars are displayed in the system menubar not in the window like on a Windows or Linux machine. This leads us to the other solution, as suggested by Levi501 and 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.
I found the 'Exit' information here: 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.
You might try creating a menubar like:
menubar = QtGui.MenuBar()
I'm basing this on the docs for the QMainWindow.menuBar() function here:
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:
http://doc.qt.io/qt-4.8/qmenubar.html#details
Hope that helps!
Correct. On MAC OS we need to use menubar like this:
self.menubar = QtGui.QMenuBar()
And not like this:
self.menubar = QtGui.QMenuBar(MainWindow)
(without the MainWindow parameter)
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. When I used the UI layout class compiled on Windows in MAC I got this issue. Solve this issue by simply recompiling the UI layout XML on MAC with the command pyside-uic AppMain.ui -o ui_AppMain.pyp
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.