MaybeALlama
MaybeALlama

Reputation: 544

Multiple checkboxes in QDockWidget

So I've been trying to make multiple checkboxes inside a QDockwidget but I seem to only be able to add one. This is what I have

def createDockWindows(self):
    cboxes = gui.QDockWidget("Cboxes", self)
    #cboxes.setWidget(gui.QCheckBox())
    cboxes.setAllowedAreas(core.Qt.LeftDockWidgetArea)

    self.c0 = gui.QCheckBox("B0")
    self.c0.setChecked(False)
    self.c0.stateChanged.connect(lambda:self.btnstate(self.c0))
    cboxes.setWidget(self.c0)

    self.c1 = gui.QCheckBox("B1")
    self.c1.setChecked(False)
    self.c1.stateChanged.connect(lambda:self.btnstate(self.c1))
    cboxes.setWidget(self.c1)

    self.addDockWidget(core.Qt.LeftDockWidgetArea, cboxes)

The output only gives me the box for B1.

I've done the dock method because I have some matplotlib graphs over to the right, a modified version of this example. If there is a better way to do this I'd be happy to change I'm just not finding much with google.

Upvotes: 0

Views: 422

Answers (1)

eyllanesc
eyllanesc

Reputation: 243887

You must create a new widget, place a layout and add the checkboxes, that new widget you should add it to the QDockWidget.

def createDockWindows(self):
    cboxes = QtGui.QDockWidget("Cboxes", self)
    cboxes.setAllowedAreas(QtCore.Qt.LeftDockWidgetArea)

    w = QtGui.QWidget()
    layout = QtGui.QVBoxLayout()
    w.setLayout(layout)

    self.c0 = QtGui.QCheckBox("B0")
    layout.addWidget(self.c0)

    self.c1 = QtGui.QCheckBox("B1")
    layout.addWidget(self.c1)

    cboxes.setWidget(w)
    self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, cboxes)

enter image description here

plus [add QSpacer]:

def createDockWindows(self):
    cboxes = QtGui.QDockWidget("Cboxes", self)
    cboxes.setAllowedAreas(QtCore.Qt.LeftDockWidgetArea)

    w = QtGui.QWidget()
    layout = QtGui.QVBoxLayout()
    w.setLayout(layout)

    self.c0 = QtGui.QCheckBox("B0")
    layout.addWidget(self.c0)

    self.c1 = QtGui.QCheckBox("B1")
    layout.addWidget(self.c1)


    spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
    layout.addItem(spacerItem)

    cboxes.setWidget(w)
    self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, cboxes)

Upvotes: 1

Related Questions