对于可以直接从Flash/Pen/Usb/Jump/Thumb驱动器运行的应用程序,为了从一台计算机移动到另一台计算机的可移植性,将用户设置存储在与正在从中运行程序(而不是每台计算机的Windows/Mac/Linux用户或系统目录)。

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

这是一个小示例程序,使用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。

UserScope而不是SystemScope并没有帮助。'.'而不是'./__settings.ini'不起作用,setPath()基本无效。
也尝试这样做无济于事:
filepath = os.getcwd() + '/__settings.ini'
self.settings.setPath(QSettings.IniFormat,QSettings.SystemScope, filepath)

引用:https://doc.qt.io/archives/qt-4.8/qsettings.html
http://www.pyside.org/docs/pyside/PySide/QtCore/QSettings.html

尽管我不知道如何适应PySide,但很有希望:
http://www.qtcentre.org/archive/index.php/t-35287.html

更新:alexisdm的答案有效,因此现在是更新的代码:
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]):

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

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

关于python - QSettings(): How to save to current working directory,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7962292/

10-08 21:42