Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding submenu in pyqt QWidget

I know its very basic question but I am little bit confused, probably I am forgetting something.

I am trying to add a sub menu "Preview" to the "Tools" in the QMenuBar()

so far this is what I am trying to do

tools = fileMenu.addMenu('&Tools')
prevAction = QtGui.QAction('Preview',self)
prevInNuke = QtGui.QAction("Using &Nuke",prevAction)
tools.addAction(prevAction)
prevAction.addAction(prevInNuke)

but I guess this is not the correct way to add a sub menu

like image 428
Ciasto piekarz Avatar asked Feb 05 '13 11:02

Ciasto piekarz


People also ask

How do you create a submenu in Python?

Creating Python Submenus A submenu is a nested menu that shows up while you move the cursor over a given menu option. To add a submenu to an application, you need to call . addMenu() on a container menu object. Say you need to add a submenu in your sample application's Edit menu.

How to create menu bar in PyQt5?

To create a menu for a PyQt5 program we need to use a QMainWindow. This type of menu is visible in many applications and shows right below the window bar. It usually has a file and edit sub menu. The top menu can be created with the method menuBar().

How menus and its options are created in PyQt5?

To create a menu, we create a menubar we call . menuBar() on the QMainWindow. We add a menu on our menu bar by calling . addMenu() , passing in the name of the menu.

What is QMenuBar?

Detailed Description. The QMenuBar class provides a horizontal menu bar. A menu bar consists of a list of pull-down menu items. You add menu items with addMenu().


1 Answers

Sub menu should be a QMenu, not QAction:

tools = fileMenu.addMenu('&Tools')
prevMenu = QtGui.QMenu('Preview',self)
prevInNuke = QtGui.QAction("Using &Nuke",prevAction)
tools.addMenu(prevMenu)
prevAction.addAction(prevInNuke)

It can be a bit more simple if you used convenience methods:

tools = fileMenu.addMenu('&Tools')
prevMenu = tools.addMenu('Preview')
prevAction = prevMenu.addAction('Using &Nuke')
like image 73
Avaris Avatar answered Nov 12 '22 01:11

Avaris