2

I saw many informations about QGroupBox but it was always inside different class or function. I would like to create a GroupBox directly inside my Gui window without creatin a new class or anything and I didn't find any solution. I need to keep my signals with the QCheckBox.

self.B_Bar1 = QtWidgets.QCheckBox("Barr1", self)
self.B_Bar1.move(1050, 150)
self.B_Bar2 = QtWidgets.QCheckBox("Barr2", self)
self.B_Bar2.move(1125, 150)
self.B_Bar3 = QtWidgets.QCheckBox("Barr3", self)
self.B_Bar3.move(1200, 150)

My QCheckBox are working well but I don't want the possibility to check these 3 box at the same time. I want only one checkable at the same moment and when I click to a new one, the previous one is unchecked.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Cheen
  • 86
  • 3
  • 12
  • Do you only want a QGroupBox so that the QCheckBox are exclusive? – eyllanesc Apr 29 '19 at 14:23
  • Yes exactly, maybe there is another way to do it ? – Cheen Apr 29 '19 at 14:25
  • 1
    You do not have to use QGroupBox which is a visual element, just use QButtonGroup: `button_group = QtWidgets.QButtonGroup(self)` `button_group.addButton(self.B_Bar1)` `button_group.addButton(self.B_Bar2)` `button_group.addButton(self.B_Bar3)` – eyllanesc Apr 29 '19 at 14:27
  • Thanks a lot. Last question, how do we do if i want to uncheck these buttons ? – Cheen Apr 29 '19 at 14:31
  • When you say exclusive implies that at least one is checked. If you want to clean the QCheckBoxs you must deactivate the exclusivity, set the QCheckBox to Qt.Unchecked, and then reestablish the exclusivity: `self.button_group.setExclusive(False)` `for btn in self.button_group.buttons(): btn.setCheckState(QtCore.Qt.Unchecked)` `self.button_group.setExclusive(True)` – eyllanesc Apr 29 '19 at 14:35
  • I don't exactly why it's not working but i will check that later, thanks. I can't check the first button and the two others aren't uncheckable, only when i click on the first button, everything is unchecked. – Cheen Apr 29 '19 at 15:00

0 Answers0