如何使用 PyQt4 将 QListWidjet 内容保存到 QSetting 或从 QSetting 加载 QListWidjet 内容?

发布于 2024-09-26 10:42:10 字数 972 浏览 10 评论 0原文

我的 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

看起来我只需要以其他方式保存数据,然后我现在就这样做。 查了很多,但没有任何线索。我确信答案是微不足道的,但我被困住了。

I've got a QListWidget in my PyQt4 app. It contains folders paths.
I want to save its contents to QSettings and load them later.
I used this code to do this:

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

But if I make so folders paths are stored in config file like:

musicfolders={0: PyQt4.QtCore.QString(u'/home/sam/Ubuntu One')}

So I have no idea how to load them then. I've tryed something like this in different variants:

def foldersLoad(self):
    folders = eval(self.tunSettings.value('musicfolders').toString())

It returns error:

TypeError: eval() arg 1 must be a string or code object

It looks like I just need to save data some other way then I do now.
Gooled a lot, but have no clue. I'm sure the answer is trivial, but I'm stuck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2024-10-03 10:42:10

解决方案非常简单。我要使用 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())

The solution is very simply. I were to use 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

and load

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())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文