Answer the question
In order to leave comments, you need to log in
How to place drop down menu in QToolBar?
Task: to place a drop-down menu.
There is a QMenuBar, and it's clear with it: you declare it, attach a QMenu to it, to that - a QAction and the drop-down menu is ready.
There is QToolBar, and everything is not at all clear with it. QAction learned to attach, but the menu did not. I opened the documentation and diligently tried to figure it out - the whole day was spent on it. To no avail.
Here is the code:
toolBar = QToolBar() #Обявляем панель инструментов
self.addToolBar(toolBar) #Крепим её к окну
Button=QToolButton(self) #Объявляем кнопку, которая должна содержать выпадающее меню
Button.setIcon(QIcon(self.style().standardIcon(QStyle.SP_TitleBarMaxButton))) #даём ей иконку
settingsMenu=QMenu()#Создаём меню, которое будет выпадать
action1 = settingsMenu.addAction("Пункт1") #Добавляем пункт меню1
settingsMenu.addSeparator() #Сепаратор, чтобы было красивше :)
action2= settingsMenu.addAction("Пункт2")#Добавляем пункт меню2
action3= settingsMenu.addAction("Пункт3")#Добавляем пункт меню3
Button.setMenu(settingsMenu) #Цепляем к кнопке меню
Button.setPopupMode(QToolButton.MenuButtonPopup) #Устанавливаем мод для отобраения меню при нажатии кнопки
self.settingsButton = toolBar.addWidget(Button)#Добавляем получившийся виджет вToolBar
Answer the question
In order to leave comments, you need to log in
In desperation, I just checked all the links in a row and in some absolutely incredible way moved here .
What is the point : you need to declare a parent for the menu, namely a button.
In my case it is like this:
settingsMenu=QMenu(Button)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question