user508402
user508402

Reputation: 492

how to make QListWidgetItem NOT tristate?

I've got a form with a QListWidget, into which I repeatedly add new items. It all woks flawless, except for on thing: the items are tristate whatever flags I pass them. Consequently the item must be clicked twice to check/uncheck them. What should I do to make them normal dual-state?

The widget is created thus:

def _locationDetails(self):
    self.locationDetails = QListWidget()
    self.locationDetails.setFixedHeight(50)
    return self.locationDetails

end the items are added as follows:

def addLocationDetail(self, text, checked = True):
    item = QListWidgetItem(text)
    item.setFlags(QtCore.Qt.ItemIsUserCheckable |
                  QtCore.Qt.ItemIsSelectable    |
                  QtCore.Qt.ItemIsEnabled)
    item.setCheckState(checked)
    self.locationDetails.addItem(item)

The code by which I invoke the addition of new items reads:

    # resolve location:
    waypoint.getLocationDetails()
    self.locationDetails.clear()
    self.addLocationDetail("location=%s"    % waypoint.location)
    self.addLocationDetail("department=%s"  % waypoint.department)
    self.addLocationDetail("country=%s"     % waypoint.country)

Upvotes: 2

Views: 442

Answers (1)

eyllanesc
eyllanesc

Reputation: 243993

The problem is caused because the setCheckState() function needs a value from the Qt::CheckState enumeration:

enum Qt::CheckState

This enum describes the state of checkable items, controls, and widgets.

Constant Value Description

Qt::Unchecked 0 The item is unchecked.

Qt::PartiallyChecked 1 The item is partially checked. Items in hierarchical models may be partially checked if some, but not all, of their children are checked.

Qt::Checked 2 The item is checked.

And since you are passing it a True value by default, it is converted to 1 which corresponds to Qt::PartiallyChecked.

A possible solution is to use the Boolean value to an appropriate value of the type Qt::CheckState:

def addLocationDetail(self, text, checked=True):
    item = QListWidgetItem(text)
    item.setFlags(QtCore.Qt.ItemIsUserCheckable |
                  QtCore.Qt.ItemIsSelectable    |
                  QtCore.Qt.ItemIsEnabled)
    item.setCheckState(QtCore.Qt.Checked if checked else QtCore.Qt.Unchecked)
    self.locationDetails.addItem(item)

Upvotes: 2

Related Questions