I would like to use several QAction
in a QMenu
like radio buttons. The active (last clicked) QAction should be checked with a dot like shown below.
(Screenshot taken from Notepad++)
Someone told me to use Qt Style Sheets to replace the arrows with dots, but I haven't found a suitable solution for this.
My code so far below.
main.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
loadUi("mainwindow.ui", self)
for setting in (self.actionSetting_1, self.actionSetting_2,
self.actionSetting_3, self.actionSetting_4,
self.actionSetting_5):
setting.setCheckable(True)
def main():
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Active Menu Action</string>
</property>
<property name="locale">
<locale language="English" country="UnitedKingdom"/>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>24</height>
</rect>
</property>
<widget class="QMenu" name="menuMenu">
<property name="title">
<string>Menu</string>
</property>
<widget class="QMenu" name="menuSettings">
<property name="title">
<string>Settings</string>
</property>
<addaction name="actionSetting_1"/>
<addaction name="actionSetting_2"/>
<addaction name="actionSetting_3"/>
<addaction name="actionSetting_4"/>
<addaction name="actionSetting_5"/>
</widget>
<addaction name="menuSettings"/>
</widget>
<addaction name="menuMenu"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionSetting_1">
<property name="text">
<string>Setting 1</string>
</property>
</action>
<action name="actionSetting_2">
<property name="text">
<string>Setting 2</string>
</property>
</action>
<action name="actionSetting_3">
<property name="text">
<string>Setting 3</string>
</property>
</action>
<action name="actionSetting_4">
<property name="text">
<string>Setting 4</string>
</property>
</action>
<action name="actionSetting_5">
<property name="text">
<string>Setting 5</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
The menu looks like this:
EDIT: Update 1: Add QActionGroup
to MainWindow
class in main.py
See One QAction checkable at time in QMenu
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
loadUi("mainwindow.ui", self)
group = QActionGroup(self.menuSettings)
actions = (self.actionSetting_1, self.actionSetting_2, self.actionSetting_3, self.actionSetting_4, self.actionSetting_5)
for setting in actions:
setting.setCheckable(True)
group.setExclusive(True)
group.triggered.connect(self.onTriggered)
def onTriggered(self, action):
print(action.text())
Result: The behaviour is unfortunately the same as before, i.e. the QAction
does not behave like radio buttons and no error message appears.