我有一个从csv文件填充的listWidget。如果用户单击listWidget中的项目,则为白色时将突出显示绿色,如果为绿色,则突出显示白色。对于列表中的几个项目,在用户进行一些交互之后,一些项目可能是绿色的,而其他项目则保持白色。有没有一种方法可以保存listWidget的状态,以便下次运行应用程序时可以读取它,并显示用户先前选择的绿色突出显示项?
def getFileName(self):
global taps
global percent
fileName = QtGui.QFileDialog.getOpenFileName()
List = open(fileName).readlines()
self.listWidget.clear()
self.listWidget.addItems(List)
for lines in List:
taps +=1
self.label_4.setText(str(taps))
percent = 100.0/taps
self._white = QtGui.QBrush(QtCore.Qt.white)
self._green = QtGui.QBrush(QtCore.Qt.green)
for i in range(0, taps):
self.listWidget.setCurrentRow(i)
currentItem = self.listWidget.currentItem()
currentItem.setBackground(self._white)
self.listWidget.setCurrentRow(0)
self.label_2.setText('0')
self.label_6.setText('0')
def listChoice(self):
#global value
global consumed
global remaining
global percent
global taps
global newpercent
currentItem = self.listWidget.currentItem()
color = currentItem.background()
if color == self._white:
currentItem.setBackground(self._green)
consumed +=1
taps = taps - 1
newpercent += percent
elif color == self._green:
currentItem.setBackground(self._white)
consumed -=1
taps = taps + 1
newpercent = newpercent - percent
currentItem.setSelected(False)
我找不到想要的东西,可能是因为它不存在,但是我确实找到了解决方法。
def saveFile(self, showDialog):
savePath = QtGui.QFileDialog.getSaveFileName(None, "Tap Room",
"Testing.csv", "CSV files (*.csv)")
with open(savePath, 'w') as theFile:
for i in xrange(self.listWidget.count()):
self.listWidget.setCurrentRow(i)
currentItem = self.listWidget.currentItem()
color = currentItem.background()
if color == self._green:
theFile.write(str(self.listWidget.item(i).text()))
theFile.write('Visited: ' +
(str(self.listWidget.item(i).text())))
else:
theFile.write(str(self.listWidget.item(i).text()))