1

I have a checkbox named "selectAllCheckBox". When this in checked state, all the checkboxes(created dynamically) inside the listview should change to checked state and when "selectAllCheckBox" checkbox is in Unchecked state, all the dynamically created checkboxes should change to unchecked state.

self.dlg.selectAllCheckBox.stateChanged.connect(self.selectAll)
def selectAll(self):
    """Select All layers loaded inside the listView"""

    model = self.dlg.DatacheckerListView1.model()
    for index in range(model.rowCount()):
        item = model.item(index)
        if item.isCheckable() and item.checkState() == QtCore.Qt.Unchecked:
            item.setCheckState(QtCore.Qt.Checked)

What the above code does it makes the dynamic checkboxes inside the listview to Checked state even when "SelectAllCheckBox" is in Unchecked state. Please help me how to solve this using python. Is there anything could be done in signal like when the checkbox is "checked" or "unchecked" to connect to a slot instead of stateChanged?

1 Answer 1

2

The stateChanged signal sends the checked state, so the slot could be re-written as:

def selectAll(self, state=QtCore.Qt.Checked):
    """Select All layers loaded inside the listView"""

    model = self.dlg.selectAllCheckBox.model()
    for index in range(model.rowCount()):
        item = model.item(index)
        if item.isCheckable():
            item.setCheckState(state)

(NB: if all the rows in the list-view have checkboxes, the isCheckable line could be omitted)

Sign up to request clarification or add additional context in comments.

4 Comments

Code works perfectly. But the difference between mine and yours is you had passed the state as an argument in the method. So could you please make it clear still? what is your code doing now?
@harinish. I gave the state argument a default value so that selectAll can be called with no arguments. When selectAll is connected to a signal that sends the state, the default argument will just be overridden.
My question is what is the default state of the "stateChanged()" signal.? And also stateChanged will be called both when the selectAllCheckBox is checked and unchecked by the user. So if selectAll is checked it will call the method "selectAll" and change the state to "Checked". But how it is unchecking all the dynamic checkboxes when the selectAll checkbox is unchecked? We didn't set any state like "Unchecked" in our method right? And also the stateChanged() signal works without passing any argument. But in doc.qt.io/qt-4.8/qcheckbox.html#stateChanged it was asked to pass an argument
The stateChanged signal has no default state. Whenever a checkbox is checked or unchecked by the user, it emits a signal that sends its current checked-state. Signals are not functions: see Signals and Slots for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.