質問

I'm programming a wizard with PyQt. On the first page I want to give the user the choice to choose two times between two options. Therefore I decided to make to Buttongroups. But though I added the first two radio buttons to the first Buttongroup and the other two to the second, the buttons are still exclusive (I can only choose one button on the page).

Here is my code:

    # 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)

Do you have any idea where's my mistake and what I have to change?

役に立ちましたか?

解決

The problem is that the QButtonGroups are never made part of the hierarchy, so they have no effect.

btw: when posting code try to add the neccessary parts to make it runnable:

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()

this should fix the problem.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top