Is there another way to remember previously selected items of a QlistWidget without making lists? in the example below I tried to do it, only I can't check the items while reading the dictionary list. here is my sample code:
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import (QWidget, QListWidget, QVBoxLayout, QApplication) from datetime import date, timedelta
class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(532, 292) self.lists={} self.itm=0 self.pushButton_ok = QtWidgets.QPushButton(Dialog) self.pushButton_ok.setGeometry(QtCore.QRect(336, 251, 88, 34)) self.pushButton_ok.setObjectName("pushButton_ok") self.pushButton_anulla = QtWidgets.QPushButton(Dialog) self.pushButton_anulla.setGeometry(QtCore.QRect(426, 251, 88, 34)) self.pushButton_anulla.setObjectName("pushButton_anulla") self.widget = QtWidgets.QWidget(Dialog) self.widget.setGeometry(QtCore.QRect(5, 13, 520, 221)) self.widget.setObjectName("widget") self.gridLayout = QtWidgets.QGridLayout(self.widget) self.gridLayout.setContentsMargins(10, 10, 10, 10) self.gridLayout.setObjectName("gridLayout") self.listWidgetdate = QtWidgets.QListWidget(self.widget) self.listWidgetdate.setObjectName("listWidgetdate") self.gridLayout.addWidget(self.listWidgetdate, 0, 1, 1, 1) self.listWidgetlav = QtWidgets.QListWidget(self.widget) self.listWidgetlav.setObjectName("listWidgetlav") item = QtWidgets.QListWidgetItem() self.listWidgetlav.addItem(item) self.gridLayout.addWidget(self.listWidgetlav, 0, 0, 1, 1)
self.listWidgetlav.currentItemChanged.connect(self.itemActivated_event)
self.list1=["B","C","D"]
for i in range(len(self.list1)):
item = QtWidgets.QListWidgetItem()
self.listWidgetlav.addItem(item)
item.setText(self.list1[i])
self.listWidgetlav.setCurrentRow(0)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton_ok.setText(_translate("Dialog", "Ok"))
self.pushButton_anulla.setText(_translate("Dialog", "Anulla"))
__sortingEnabled = self.listWidgetdate.isSortingEnabled()
self.listWidgetdate.setSortingEnabled(False)
self.listWidgetdate.setSortingEnabled(__sortingEnabled)
__sortingEnabled = self.listWidgetlav.isSortingEnabled()
self.listWidgetlav.setSortingEnabled(False)
item = self.listWidgetlav.item(0)
item.setText(_translate("Dialog", "A"))
self.listWidgetlav.setSortingEnabled(__sortingEnabled)
def load_item(self):
self.list2=self.dates(date(2019,5,3),date(2019,5,6))
self.listWidgetdate.clear()
for i in range(0,self.listWidgetdate.count()):
self.listWidgetdate.takeItem(i)
for i in range(len(self.list2)):
item1 = QtWidgets.QListWidgetItem()
self.listWidgetdate.addItem(item1)
item1.setText(self.list2[i])
item1.setFlags(QtCore.Qt.ItemIsUserCheckable|QtCore.Qt.ItemIsEnabled)
item1.setCheckState(QtCore.Qt.Unchecked)
def itemActivated_event(self):
self.make_settigs(self.listWidgetlav.currentRow())
self.load_item()
print(self.listWidgetlav.currentRow())
def on_clicked(self):
self.list_of_imp=[]
for row in range(0, self.listWidgetdate.count()):
it = self.listWidgetdate.item(row)
if it.checkState() == QtCore.Qt.Checked:
# print(it.text())
self.list_of_imp.append(True)
else:self.list_of_imp.append(False)
return self.list_of_imp
def dates(self,datein,dateout):
self.list_dates=[]
# start_date = datein
# end_date = dateout
st_date = datein
ed_date = dateout
delta = ed_date - st_date
for i in range(delta.days+1):
day =st_date + timedelta(days=i)
e=str(day)
self.list_dates.append(e)
return self.list_dates
def make_settigs(self,ind):
self.tmp=self.on_clicked()
self.lists['lst'+str(self.itm)]=self.tmp
self.current=ind
checked=2
unchecked=0
----- The problem is here ------
try:
if len(self.lists['lst'+str(self.current)])!=0:
for i in range(len(self.lists['lst'+str(self.current)])):
it = self.listWidgetdate.item(i)
if (self.lists['lst'+str(self.current)][i])==True:
if it.checkState() == QtCore.Qt.Unchecked:
print(it.checkState, "------checked--------")
it.setCheckState(checked)
else:
it.setCheckState(checked)
elif (self.lists['lst'+str(self.current)][i])==False:
if it.checkState() == QtCore.Qt.Checked:
print(it.checkState, "-----unchecked---------")
it.setCheckState(unchecked)
else:
it.setCheckState(unchecked)
except: print("We don t have the list yet")
self.lists['lst'+str(self.itm)]=self.tmp
self.itm=ind
print(self.lists)
if name == "main": import sys app = QtWidgets.QApplication(sys.argv) Dialog = QtWidgets.QWidget() ui = Ui_Dialog() ui.setupUi(Dialog) Dialog.show() sys.exit(app.exec_())
Aucun commentaire:
Enregistrer un commentaire