我的PyQt4应用程序中有一个QListWidget。它包含文件夹路径。 我想将其内容保存到QSettings并稍后加载它们。 我使用此代码执行此操作:
def foldersSave(self):
folders = {} '''create dict to store data'''
foldersnum = self.configDialog.FolderLIST.count() '''get number of items'''
if foldersnum:
for i in range(foldersnum):
folders[i] = self.configDialog.FolderLIST.item(i).text() '''save items text to dict'''
return str(folders) '''return string of folders to store in QSettings'''
return None
但是,如果我这样设置文件夹路径存储在配置文件中,如:
musicfolders={0: PyQt4.QtCore.QString(u'/home/sam/Ubuntu One')}
所以我不知道如何加载它们。我尝试过不同的变体:
def foldersLoad(self):
folders = eval(self.tunSettings.value('musicfolders').toString())
它返回错误:
TypeError: eval() arg 1 must be a string or code object
看起来我只需要以其他方式保存数据。 很多,但没有任何线索。我确定答案是微不足道的,但我被困住了。
答案 0 :(得分:0)
解决方案非常简单。我当时使用的是QStringList。
def foldersSave(self):
folders = QtCore.QStringList()
foldersnum = self.configDialog.FolderLIST.count()
if foldersnum:
for i in range(foldersnum):
print (i, " position is saved: ", self.configDialog.FolderLIST.item(i).text())
folders.append(self.configDialog.FolderLIST.item(i).text())
return folders
return None
并加载
def foldersLoad(self):
folders = QtCore.QStringList()
folders = self.tunSettings.value('musicfolders', None).toStringList()
if folders.count():
foldersnum = folders.count()
for i in range(foldersnum):
self.configDialog.FolderLIST.addItem(folders.takeFirst())