Search code examples
pythonpyqtpyqt4qcheckbox

In PyQt, how to pass different properties of QCheckBox


builing on teh following example, how can i pass/access more properties then the 'state' of the QCheckBox? In this case I am looking to build a more dynamic function that can be used for multiple buttons and saves all status in a dict.

something like this (I am mainly looking for the correct name of 'objectName' as it is used with 'state' in the 'ILCheckbox_changed' definition, and maybe more generally how I can find whcih other properties I can pass)

import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class SelectionWindow(QMainWindow):
    def __init__(self, parent=None):
        super(SelectionWindow, self).__init__()

        self.ILCheck = False
        ILCheckbox = QCheckBox(self)
        ILCheckbox.setCheckState(Qt.Unchecked)

        self.check_box_dict = {}
        ILCheckbox.stateChanged.connect(self.ILCheckbox_changed)

        MainLayout = QGridLayout()
        MainLayout.addWidget(ILCheckbox, 0, 0, 1, 1)

        self.setLayout(MainLayout)

    def ILCheckbox_changed(self, state, objectName):
        self.ILCheck = (state == Qt.Checked)

        print(self.ILCheck)
        self.check_box_dict[objectName] = self.ILCheck

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = SelectionWindow()

    window.show()
    sys.exit(app.exec_())

Solution

  • In every slot you can access the object that sends the signal with the function self.sender()

    def ILCheckbox_changed(self, state):
        self.ILCheck = (state == Qt.Checked)
        print(self.ILCheck)
        self.check_box_dict[self.sender()] = self.ILCheck
        print(self.check_box_dict)