from PySide import QtCore
from PySide import QtGui
class UI(QtGui.QDialog):
def __init__(self):
super(UI,self).__init__()
self.setWindowTitle('Test UI 2000')
self.create_layout()
def create_layout(self):
mainLayout = QtGui.QVBoxLayout()
self.setLayout(mainLayout)
fruitLabel = QtGui.QLabel('Fruit')
junkLabel = QtGui.QLabel('Junk')
buttonGroup1 = QtGui.QButtonGroup()
radioButtonA = QtGui.QRadioButton('Apple')
radioButtonB = QtGui.QRadioButton('Pear')
buttonGroup1.addButton(radioButtonA)
buttonGroup1.addButton(radioButtonB)
buttonGroup2 = QtGui.QButtonGroup()
radioButtonC = QtGui.QRadioButton('Hotdog')
radioButtonD = QtGui.QRadioButton('Hamburger')
buttonGroup2.addButton(radioButtonC)
buttonGroup2.addButton(radioButtonD)
mainLayout.addWidget(fruitLabel)
mainLayout.addWidget(radioButtonA)
mainLayout.addWidget(radioButtonB)
mainLayout.addWidget(junkLabel)
mainLayout.addWidget(radioButtonC)
mainLayout.addWidget(radioButtonD)
if __name__ == '__main__':
try:
ui.close()
except:
pass
ui = UI()
ui.setAttribute(QtCore.Qt.WA_DeleteOnClose)
ui.show()
我一直在努力理解为什么在将两组 QRadioButtons 添加到各自的 QButtonGroup 之后,它们仍然像在同一个父级下一样运行。我需要“水果”QRadioButtons 独立于“垃圾”QRadioButtons 工作。
根据文档,“如果启用了自动排他性(默认情况下),则属于同一父小部件的单选按钮的行为就像它们是同一排他性按钮组的一部分一样”。
在将我的 QRadioButtons 添加到我的 QVBoxLayout 之后,我是否以某种方式覆盖了 QButtonGroup?