Python Forum

Full Version: Save settings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I want to ask you how to save options. I have main window with charts and calculations. When I click Settings button, second window is open.

The code is too long, I will post just parts.

Main window:

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.canvas = MplCanvas(self, width=5, height=4, dpi=100)
        self.setCentralWidget(self.canvas)
        #self.setMinimumSize(1024,600)  
        self.setWindowTitle("name")

        self.window1 = OptionsWindow()   # second window with options

        toolbar = NavigationToolbar(self.canvas, self)
        layout = QtWidgets.QVBoxLayout()
      
        buttonbox = QHBoxLayout()   
        buttonbox2 = QHBoxLayout() 
        buttonbox3 = QHBoxLayout()
 
        btn0 = QPushButton("Acquire data")
        btn0.clicked.connect(self.btn_0_clicked)
        btn0.setMinimumHeight(40)
        btn0.setFont(QFont('Arial', 10))
        buttonbox.addWidget(btn0)
.......etc.


I have second window as settings:

class OptionsWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setMinimumSize(700,520)   
        self.setWindowTitle("Settings")

        label1 = QLabel("Samples: ", self)
        label1.setGeometry(20, 20, 80, 20)
        edit1 = QLineEdit("30000", self)
        edit1.setGeometry(100, 20, 50, 20)

        label2 = QLabel("SPS: ", self)
        label2.setGeometry(20, 50, 80, 20)
        edit2 = QLineEdit("30000", self)
        edit2.setGeometry(100, 50, 50, 20)

# I have here two buttons - Save and Cancel

        save_button = QPushButton("Save", self)
        save_button.setGeometry(20, 480, 100, 30)
        save_button.clicked.connect(self.save_button_clicked)

        close_button = QPushButton("Cancel", self)
        close_button.setGeometry(600, 480, 100, 30)
        close_button.clicked.connect(self.close_button_clicked)

        #-------------------------------------------------------------------------------------------------------

    def save_button_clicked(self):            # stisknutí tlačíka přehrání signálu
        self.save_settings()

    def save_settings(self):
        print("save settings")

    def close_button_clicked(self):            # stisknutí tlačíka přehrání signálu
        self.close_settings()
    
    def close_settings(self):
        self.close()
...etc.

How to use Settings to save values in edit1 and edit2? How to restore saved values? I think there will be conflict between restore settings and value "30000" in edit1 = QLineEdit("30000", self).

Thanks for help.
In a file? In a database?
You can use QSettings
Hi, thanks, QSettings works fine for me.

Maybe this will help somebody else:

Restore settings from registry:

self.settings = QtCore.QSettings('name1', 'name2')
label5 = QLabel("High pass: ", self)
label5.setGeometry(20, 160, 80, 20)
self.edit5 = QLineEdit("1500", self)
self.edit5.setGeometry(100, 160, 50, 20)
if self.settings.contains("velocity_high_pass"):
  self.edit5.setText(self.settings.value("velocity_high_pass"))
else:    
  self.edit5 = QLineEdit("1500", self)   
Save settings:
self.settings.setValue("velocity_high_pass", self.edit5.text())