一个窗口中有多个QButtonGroup

时间:2012-12-05 20:28:17

标签: python radio-button pyqt radio-group buttongroup

我正在使用PyQt编写一个向导。在第一页上,我想让用户选择在两个选项之间选择两次。 所以我决定向Buttongroups做。但是虽然我将前两个单选按钮添加到第一个Buttongroup而另外两个添加到第二个,但按钮仍然是独占的(我只能在页面上选择一个按钮)。

这是我的代码:

    # Setup UI
    layout = QtGui.QVBoxLayout()

    gBBackupFromIntExt = QtGui.QGroupBox()
    layout.addWidget(gBBackupFromIntExt)

    bGBackupFromIntExt = QtGui.QButtonGroup()

    self.rBBackupFromExt = QtGui.QRadioButton()
    bGBackupFromIntExt.addButton (self.rBBackupFromExt)
    layout.addWidget(self.rBBackupFromExt)

    self.rBBackupFromInt = QtGui.QRadioButton()
    bGBackupFromIntExt.addButton (self.rBBackupFromInt)
    layout.addWidget(self.rBBackupFromInt)

    gBBackupToIntExt = QtGui.QGroupBox()
    layout.addWidget(gBBackupToIntExt)

    bGBackupToIntExt = QtGui.QButtonGroup()

    self.rBBackupToExt = QtGui.QRadioButton()
    bGBackupToIntExt.addButton (self.rBBackupToExt)
    layout.addWidget(self.rBBackupToExt)

    self.rBBackupToInt = QtGui.QRadioButton()
    bGBackupToIntExt.addButton (self.rBBackupToInt)
    layout.addWidget(self.rBBackupToInt)

你知道我的错误在哪里以及我必须改变什么吗?

1 个答案:

答案 0 :(得分:4)

问题是QButtonGroups永远不会成为层次结构的一部分,所以它们没有效果。

btw:发布代码时,尝试添加必要的部分以使其可运行:

from PyQt4 import QtGui
import sys

class Test(QtGui.QWidget):
    def __init__(self):
        super().__init__()
        layout = QtGui.QVBoxLayout(self)

        gBBackupFromIntExt = QtGui.QGroupBox()
        layout.addWidget(gBBackupFromIntExt)

        bGBackupFromIntExt = QtGui.QButtonGroup(self)

        self.rBBackupFromExt = QtGui.QRadioButton()
        bGBackupFromIntExt.addButton(self.rBBackupFromExt)
        layout.addWidget(self.rBBackupFromExt)

        self.rBBackupFromInt = QtGui.QRadioButton()
        bGBackupFromIntExt.addButton(self.rBBackupFromInt)
        layout.addWidget(self.rBBackupFromInt)

        gBBackupToIntExt = QtGui.QGroupBox()
        layout.addWidget(gBBackupToIntExt)

        bGBackupToIntExt = QtGui.QButtonGroup(self)

        self.rBBackupToExt = QtGui.QRadioButton()
        bGBackupToIntExt.addButton (self.rBBackupToExt)
        layout.addWidget(self.rBBackupToExt)

        self.rBBackupToInt = QtGui.QRadioButton()
        bGBackupToIntExt.addButton (self.rBBackupToInt)
        layout.addWidget(self.rBBackupToInt)

a = QtGui.QApplication(sys.argv)
t = Test()
t.show()
a.exec()

这应该可以解决问题。