QSettings():如何保存到当前工作目录 [英] QSettings(): How to save to current working directory

查看:55
本文介绍了QSettings():如何保存到当前工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于可以直接从闪存/笔/USB/jump/thumb 驱动器运行的应用程序,为了从一台机器移动到另一台机器的便携性,将用户设置存储在同一台记忆棒上是有意义的运行程序的目录(而不是 Windows/Mac/Linux 用户或每台机器的系统目录).

For an app that can be run directly from a flash/pen/usb/jump/thumb drive, for portability in moving from one machine to another it can make sense for user settings to be stored on the memory stick in the same directory that the program is being run from (rather than Windows/Mac/Linux user or system dirs per machine).

QSettings() 很方便,但是可以告诉它使用当前工作目录吗?

QSettings() is handy, however, can it be told to use the current working directory?

这是一个使用 QSettings() 保持其屏幕位置从运行到运行的小示例程序:

Here's a little example program that keeps its screen position from run to run using QSettings():

from PySide import QtGui, QtCore
from PySide.QtGui import QTabWidget, QApplication
from PySide.QtCore import QSettings

class AbcApp(QTabWidget):
    def __init__(self):
        super(AbcApp, self).__init__()

        self.settings = QSettings(QSettings.IniFormat,QSettings.SystemScope, '__MyBiz', '__settings')
        self.settings.setFallbacksEnabled(False)    # File only, not registry or or.

        # setPath() to try to save to current working directory
        self.settings.setPath(QSettings.IniFormat,QSettings.SystemScope, './__settings.ini')

        # Initial window size/pos last saved
        self.resize(self.settings.value("size", QtCore.QSize(270, 225)))
        self.move(self.settings.value("pos", QtCore.QPoint(50, 50)))

        self.tab = QtGui.QWidget()
        self.textEdit = QtGui.QTextEdit(self.tab)
        self.textEdit.setHtml('<font color=grey>[QTextEdit area]</font><p><center><font color=blue size=4><b>Allo Woyld</b></font></center>')
        self.addTab(self.tab, 'tab1')

    def closeEvent(self, e):
        # Write window size and position to config file
        self.settings.setValue("size", self.size())
        self.settings.setValue("pos", self.pos())

        e.accept()

if __name__ == '__main__':
    app = QApplication('')
    frame = AbcApp()
    frame.show()
    app.exec_()

创建此 .ini 文件是因为我目前正在 Windows 上运行:C:\Documents and Settings\All Users\Application Data__MyBiz__settings.ini.

This .ini file is created since I happen to be running on Windows at the moment: C:\Documents and Settings\All Users\Application Data__MyBiz__settings.ini.

UserScope 而不是 SystemScope 没有帮助.
'.' 代替 './__settings.ini' 没用,setPath() 基本没效果.
也试过这个无济于事:

UserScope instead of SystemScope does not help.
'.' instead of './__settings.ini' did not work, setPath() basicaly no effect.
Also tried this to no avail:

filepath = os.getcwd() + '/__settings.ini'
self.settings.setPath(QSettings.IniFormat,QSettings.SystemScope, filepath)

参考:https://doc.qt.io/archives/qt-4.8/qsettings.htmlhttp://www.pyside.org/docs/pyside/PySide/QtCore/QSettings.html

有前途虽然我不知道如何适应PySide:
http://www.qtcentre.org/archive/index.php/t-35287.html

Promising though I don't know how to adapt to PySide:
http://www.qtcentre.org/archive/index.php/t-35287.html

更新:来自 alexisdm 的答案有效,所以现在更新代码:

Update: The answer from alexisdm works, so here's the updated code now:

from PySide import QtGui, QtCore
from PySide.QtGui import QTabWidget, QApplication
from PySide.QtCore import QSettings

class AbcApp(QTabWidget):
    def __init__(self):
        super(AbcApp, self).__init__()

        self.settings = QSettings('settings.ini', QSettings.IniFormat)
        self.settings.setFallbacksEnabled(False)    # File only, no fallback to registry or or.

        # Initial window size/pos last saved if available
        self.resize(self.settings.value('size', QtCore.QSize(270, 225)))
        self.move(self.settings.value('pos', QtCore.QPoint(50, 50)))

        self.tab = QtGui.QWidget()
        self.textEdit = QtGui.QTextEdit(self.tab)
        self.textEdit.setHtml('<font color=grey>[QTextEdit area]</font><p><center><font color=blue size=4><b>Allo Woyld</b></font></center>')
        self.addTab(self.tab, 'tab1')

    def closeEvent(self, e):
        self.settings.setValue('size', self.size())
        self.settings.setValue('pos', self.pos())
        e.accept()

if __name__ == '__main__':
    app = QApplication('')
    frame = AbcApp()
    frame.show()
    app.exec_()

推荐答案

你可以像这样使用重载 class QSettings(fileName, format[, parent=None]):

You can use that overload class QSettings(fileName, format[, parent=None]) like this:

self.settings = QSettings("__settings.ini", QSettings.IniFormat)

如果路径是相对路径,则文件已经在当前工作目录中打开,但这可能不是您想要的.
您可以尝试使用这些答案之一来使用脚本所在的目录.

If the path is relative, the file will already be opened in the current working directory, but that may not be what you want.
You may try one of these answers to use the directory where the script is instead.

这篇关于QSettings():如何保存到当前工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆