Skip to content

multicheck

sleap.gui.widgets.multicheck

Module for Qt Widget to show multiple checkboxes for selecting.

Example: ::

>>> mc = MultiCheckWidget(count=5, selected=[0,1],title="My Items")
>>> mc.selectionChanged.connect(window.plot)
>>> window.layout.addWidget(mc)

Classes:

Name Description
MultiCheckWidget

Qt Widget to show multiple checkboxes for a sequence of numbers.

MultiCheckWidget

Bases: QGroupBox

Qt Widget to show multiple checkboxes for a sequence of numbers.

Parameters:

Name Type Description Default
count int

The number of checkboxes to show.

required
title Optional[str]

Display title for group of checkboxes.

''
selected Optional[List]

List of checkbox numbers to initially check.

None
default Optional[bool]

Whether to default boxes as checked.

False

Methods:

Name Description
boundingRect

Method required by Qt.

getSelected

Method to get list of the checked checkboxes.

paint

Method required by Qt.

setSelected

Method to set some checkboxes as checked.

Source code in sleap/gui/widgets/multicheck.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
class MultiCheckWidget(QGroupBox):
    """Qt Widget to show multiple checkboxes for a sequence of numbers.

    Args:
        count: The number of checkboxes to show.
        title: Display title for group of checkboxes.
        selected: List of checkbox numbers to initially check.
        default: Whether to default boxes as checked.
    """

    def __init__(
        self,
        *args,
        count: int,
        title: Optional[str] = "",
        selected: Optional[List] = None,
        default: Optional[bool] = False,
        **kwargs,
    ):
        super(MultiCheckWidget, self).__init__(*args, **kwargs)

        # QButtonGroup is the logical container
        # it allows us to get list of checked boxes more easily
        self.check_group = QButtonGroup()
        self.check_group.setExclusive(False)  # more than one can be checked

        if title != "":
            self.setTitle(title)
            self.setFlat(False)
        else:
            self.setFlat(True)

        if selected is None:
            selected = list(range(count)) if default else []

        check_layout = QGridLayout()
        self.setLayout(check_layout)
        for i in range(count):
            check = QCheckBox("%d" % (i))
            # call signal/slot on self when one of the checkboxes is changed
            check.stateChanged.connect(lambda e: self.selectionChanged.emit())
            self.check_group.addButton(check, i)
            check_layout.addWidget(check, i // 8, i % 8)
        self.setSelected(selected)

    """
    selectionChanged signal sent when a checkbox gets a stateChanged signal
    """
    selectionChanged = Signal()

    def getSelected(self) -> list:
        """Method to get list of the checked checkboxes.

        Returns:
            list of checked checkboxes
        """
        selected = []
        for check_button in self.check_group.buttons():
            if check_button.isChecked():
                selected.append(self.check_group.id(check_button))
        return selected

    def setSelected(self, selected: list):
        """Method to set some checkboxes as checked.

        Args:
            selected: List of checkboxes to check.

        Returns:
            None
        """
        for check_button in self.check_group.buttons():
            if self.check_group.id(check_button) in selected:
                check_button.setChecked(True)
            else:
                check_button.setChecked(False)

    def boundingRect(self) -> QRectF:
        """Method required by Qt."""
        return QRectF()

    def paint(self, painter, option, widget=None):
        """Method required by Qt."""
        pass

boundingRect()

Method required by Qt.

Source code in sleap/gui/widgets/multicheck.py
95
96
97
def boundingRect(self) -> QRectF:
    """Method required by Qt."""
    return QRectF()

getSelected()

Method to get list of the checked checkboxes.

Returns:

Type Description
list

list of checked checkboxes

Source code in sleap/gui/widgets/multicheck.py
68
69
70
71
72
73
74
75
76
77
78
def getSelected(self) -> list:
    """Method to get list of the checked checkboxes.

    Returns:
        list of checked checkboxes
    """
    selected = []
    for check_button in self.check_group.buttons():
        if check_button.isChecked():
            selected.append(self.check_group.id(check_button))
    return selected

paint(painter, option, widget=None)

Method required by Qt.

Source code in sleap/gui/widgets/multicheck.py
 99
100
101
def paint(self, painter, option, widget=None):
    """Method required by Qt."""
    pass

setSelected(selected)

Method to set some checkboxes as checked.

Parameters:

Name Type Description Default
selected list

List of checkboxes to check.

required

Returns:

Type Description

None

Source code in sleap/gui/widgets/multicheck.py
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def setSelected(self, selected: list):
    """Method to set some checkboxes as checked.

    Args:
        selected: List of checkboxes to check.

    Returns:
        None
    """
    for check_button in self.check_group.buttons():
        if self.check_group.id(check_button) in selected:
            check_button.setChecked(True)
        else:
            check_button.setChecked(False)