vy32 vy32 -4 years ago 629
Python Question

PyQt5 — swipe to check/uncheck in QListWidget

I would like to set a QListWidget in PyQt5 to check or uncheck the check boxes when I swipe.

Here is my interface:

enter image description here

And my code:

import sys
from PyQt5.QtCore import QDate, QSize, Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class VerifyDialog(QDialog):
def __init__(self, parent=None):
super(VerifyDialog, self).__init__(parent)

self.listWidget = QListWidget()

for i in range(100):
item = QListWidgetItem("Item %i" % i)
# could be Qt.Unchecked; setting it makes the check appear
item.setCheckState(Qt.Checked)
self.listWidget.addItem(item)

runButton = QPushButton("Run")
runButton.clicked.connect(self.exec)

cancelButton = QPushButton("Cancel")
cancelButton.clicked.connect(self.close)

horizontalLayout = QHBoxLayout()
horizontalLayout.addWidget(self.listWidget, 1)

buttonsLayout = QHBoxLayout()
buttonsLayout.addStretch(1)
buttonsLayout.addWidget(runButton)
buttonsLayout.addWidget(cancelButton)

mainLayout = QVBoxLayout()
mainLayout.addLayout(horizontalLayout)
mainLayout.addSpacing(12)
mainLayout.addLayout(buttonsLayout)

self.setLayout(mainLayout)
self.setWindowTitle("Config Dialog")
self.show()

if __name__=="__main__":
app = QApplication(sys.argv)
dialog = VerifyDialog()
sys.exit(app.exec_())


I want to click on Item 4 and have it uncheck (it's easier than clicking the box), and then I want to drag down and have lots of other items uncheck.

Answer Source

You must use the itemEntered signal that sends you the object under the mouse.

self.listWidget.itemEntered.connect(lambda item: item.setCheckState(Qt.Checked if item.checkState()==Qt.Unchecked else Qt.Unchecked))

Complete code:

import sys
from PyQt5.QtCore import QDate, QSize, Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class VerifyDialog(QDialog):
    def __init__(self, parent=None):
        super(VerifyDialog, self).__init__(parent)

        self.listWidget = QListWidget()

        self.listWidget.itemEntered.connect(lambda item: item.setCheckState(Qt.Checked if item.checkState()==Qt.Unchecked else Qt.Unchecked))


        for i in range(100):
            item = QListWidgetItem("Item %i" % i)
            # could be Qt.Unchecked; setting it makes the check appear
            item.setCheckState(Qt.Checked)
            self.listWidget.addItem(item)

        runButton = QPushButton("Run")
        runButton.clicked.connect(self.exec)

        cancelButton = QPushButton("Cancel")
        cancelButton.clicked.connect(self.close)

        horizontalLayout = QHBoxLayout()
        horizontalLayout.addWidget(self.listWidget, 1)

        buttonsLayout = QHBoxLayout()
        buttonsLayout.addStretch(1)
        buttonsLayout.addWidget(runButton)
        buttonsLayout.addWidget(cancelButton)

        mainLayout = QVBoxLayout()
        mainLayout.addLayout(horizontalLayout)
        mainLayout.addSpacing(12)
        mainLayout.addLayout(buttonsLayout)

        self.setLayout(mainLayout)
        self.setWindowTitle("Config Dialog")
        self.show()


if __name__=="__main__":
    app = QApplication(sys.argv)
    dialog = VerifyDialog()
    sys.exit(app.exec_())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download