QButtonGroup 没有使复选框独占

2022-01-12 00:00:00 python python-3.x pyqt pyqt5 qbuttongroup

问题描述

我正在尝试使用 QGroupBox 制作一组排他性复选框(据我所知,默认情况下是排他性的),但是当我运行我的程序时,复选框不是排他性的,并且它们的行为正常.

I am attempting to make a set of exclusive checkboxes, using QGroupBox (which, as I understand it, is exclusive by default), but when I run my program, the checkboxes are not exclusive and behave as they normally would.

skillP = QCheckBox("Passive")
skillCb = QCheckBox("Combat")
skillCm = QCheckBox("Command")
skillP.setChecked(True)
addskillG = QButtonGroup()
addskillG.addButton(skillP)
addskillG.addButton(skillCm)
addskillG.addButton(skillCb)

是不是我做错了什么?


解决方案

这个问题是因为垃圾收集器从内存中移除了变量QButtonGroup,要解决这个问题你必须给这个对象传递一个父对象:

The problem is caused because the garbage collector removes from the memory the variable QButtonGroup, to solve that problems you must pass a parent to this object:

addskillG = QButtonGroup(self)

相关文章