A
A
Anastasia Taube2020-04-17 00:36:14
Python
Anastasia Taube, 2020-04-17 00:36:14

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


And everything would be fine, but as a result, I just get an absolutely beautiful button that shows absolutely nothing.
spoiler
5e98cea4327f3166192400.png

I feel like I've been going around and around all day but still can't seem to get that couple of lines that "make it all right".
Help, please: how to properly declare a dropdown menu in QToolBar? :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anastasia Taube, 2020-04-17
@Tayaki

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 question

Ask a Question

731 491 924 answers to any question