0

So I'm developing a GUI that consists of many widgets such as checkbox, buttons, radio-buttons, combo-box using Python Qt5. This GUI is to perform some calcualtion based on user choices. I'm trying to figure out how to go about saving all this data in a file and then repopulating those widgets when the user opens the same file.

My research lead me to QSetting class of Qt5 but that only saves the states of widgets.

For Example:

    def writeSettings(self):
    self.settings = QSettings("Moose Soft", "Clipper")
    self.settings.beginGroup("MainWindow")
    self.settings.setValue("size", self.size())
    self.settings.setValue("pos", self.pos())
    self.settings.endGroup()

def readSettings(self):
    self.settings = QSettings("Moose Soft", "Clipper")
    self.settings.beginGroup("MainWindow")
    self.resize(settings.value("size", QSize(400, 400)).toSize())
    self.move(settings.value("pos", QPoint(200, 200)).toPoint())
    self.settings.endGroup()

But this is not doing what I want (or maybe I'm not aware of some of its functionalities?)

Summary: What I want is to have the user save the file and come back to that file after a while and the file should know which widgets was checked/used and repopulate only those. How do I approach this issue?

By the way, I'm new to GUI design :-/

  • I don't do python, but in all the other languages I've used (programming since 80s) working with flat files/ini files/etc can be a pain unless there is a good/great wrapper library/class. I'd look at using a small db like SQLite. – ivanivan Apr 26 at 16:47
  • Are you aware of the Model-View-Controller pattern? That's an extremely important design pattern to know if you're doing anything with GUIs - I'd recommend taking the time to look into it, since the concepts of GUI design versus data persistence (e.g. file I/O) are entirely separate concerns, and it's generally recommended that you keep code related to those things completely separate from each other. Data persistence is a problem which is better solved completely independently without using anything from your GUI framework. – Ben Cottrell Apr 26 at 17:05
  • Thank you @Ivan & @Ben! I was trying to avoid implementing SQL in the GUI but I have to look into it. Ben I briefly looked into it right now. Looks like I need to get deeply familiar with this software architectural pattern. Thank you! – Prometheus Apr 26 at 17:38
0

An effective technique ive used is to create a dict where the keys are the gui elements and their parameters, and the values are the arguments the user put in those parameters.

Example

data = {
    frame1:{textBox1:"input text", textBox2:"input text"}, 
    frame2:{Button1: 0, Button2: 1},}

If you want to get really slick you can rename the gui elements with the actual data you are collecting

  • This answer doesn't talk about saving and reloading, which seem pretty key to the OP's question. – Ed Grimm May 4 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.