Checkable [QGroupBox] reverse False/True to Block/Unblock The content items

221 views Asked by At

Normal operation:

  • If checked> all fields are active!
  • If not checked > All fields are blocked!

Any way to reverse this? I want to block all widgets inside the QGroupBox when the user checked.

1

There are 1 answers

2
eyllanesc On BEST ANSWER

A possible solution is only to change the painting, that is, if the state of the checked property of the QGroupBox is true then the checkbox is not painted, and otherwise if the checkbox is painted.

import sys
from PyQt5 import QtWidgets


class GroupBox(QtWidgets.QGroupBox):
    def paintEvent(self, event):
        painter = QtWidgets.QStylePainter(self)
        option = QtWidgets.QStyleOptionGroupBox()
        self.initStyleOption(option)
        if self.isCheckable():
            option.state &= ~QtWidgets.QStyle.State_Off & ~QtWidgets.QStyle.State_On
            option.state |= (
                QtWidgets.QStyle.State_Off
                if self.isChecked()
                else QtWidgets.QStyle.State_On
            )
        painter.drawComplexControl(QtWidgets.QStyle.CC_GroupBox, option)


def main():
    app = QtWidgets.QApplication(sys.argv)
    groupbox = GroupBox(checkable=True)
    groupbox.resize(640, 480)
    groupbox.show()

    vbox = QtWidgets.QVBoxLayout()

    for i in range(10):
        le = QtWidgets.QLineEdit()
        vbox.addWidget(le)

    groupbox.setLayout(vbox)

    sys.exit(app.exec_())


if __name__ == "__main__":
    pass
    main()