QSettings():如何保存到当前工作目录

时间:2011-11-01 03:53:14

标签: python pyqt4 pyside

对于可以直接从闪存/笔/ USB /跳/拇指驱动器运行的应用程序,为了便于从一台机器移动到另一台机器,可以将用户设置存储在记忆棒上同样有意义正在运行程序的目录(而不是每台机器的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_()

1 个答案:

答案 0 :(得分:8)

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

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

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