Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QSettings(): How to save to current working directory

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() is handy, however, can it be told to use the current working directory?

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_()

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 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)

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

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


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_()
like image 641
gseattle Avatar asked Nov 01 '11 03:11

gseattle


1 Answers

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.

like image 154
alexisdm Avatar answered Oct 14 '22 05:10

alexisdm