I tried to recreate what you want to do.
Try it:
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Window(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.lineEditTotalPDFnumber = QtWidgets.QLineEdit('QLineEdit')
self.lineEditTotalPDFnumber.setReadOnly(True)
self.listWidgetPDFlist = QtWidgets.QListWidget()
self.vlayout = QtWidgets.QVBoxLayout()
self.vlayout.addWidget(self.listWidgetPDFlist)
self.vlayout.addWidget(self.lineEditTotalPDFnumber)
self.btnAddItems = QtWidgets.QPushButton()
self.btnAddItems.setText('add items')
self.vlayout.addWidget(self.btnAddItems)
self.btnAddItems.clicked.connect(self.addItems)
self.btnPrintItems = QtWidgets.QPushButton()
self.btnPrintItems.setText('print the total number of checked items')
self.vlayout.addWidget(self.btnPrintItems)
self.btnPrintItems.clicked.connect(self.printItems)
self.setLayout(self.vlayout)
def addItems(self):
Files = ["file1", "file2", "file3",]
print("\nFiles ", Files)
self.ListFilesInViewer(Files)
def ListFilesInViewer(self, Files):
for item1 in Files:
item = QtWidgets.QListWidgetItem(item1)
print("\titem =",item)
item.setCheckState(QtCore.Qt.Unchecked) #Unchecked
item.setText('`{}` - QListWidget item № `{}`'.format(str(item1), str(self.listWidgetPDFlist.count())))
self.listWidgetPDFlist.addItem(item) # listWidgetPDFlist
def printItems(self):
checkedItem = 0
for index in range(self.listWidgetPDFlist.count()):
if self.listWidgetPDFlist.item(index).checkState() == QtCore.Qt.Checked:
print("checked item ==>{}, index={}".format(self.listWidgetPDFlist.item(index), index))
checkedItem += 1
self.lineEditTotalPDFnumber.setText("checked items --> {}".format(str(checkedItem)))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.resize(500, 400)
window.show()
sys.exit(app.exec_())