Results 1 to 3 of 3

Thread: Save QSettings when OK button is pressed

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2015
    Posts
    28
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Save QSettings when OK button is pressed

    I'm in the process of creating a Settings dialog for my PyQt5 application. I have a button in the MainWindow toolbar that pops up a dialog box, containing a checkbox and a buttonBox (ui file). The idea is to load QSettings and show the appropriate settings (checked/unchecked). Changes can be made, and those are to be saved when you click the OK button.

    So how do you do this? I thought that one way would be to "intercept" the OK clicking event and insert a few lines of code before the dialog is closed. I tried this:

    Qt Code:
    1. class SettingsDialog(QDialog, Ui_SettingsDialog):
    2. def __init__(self, parent=None):
    3. super(SettingsDialog, self).__init__(parent)
    4. self.setupUi(self)
    5. self.settings = QSettings(QSettings.IniFormat, QSettings.SystemScope, 'someBiz', '__settings')
    6. self.settings.setFallbacksEnabled(False)
    7.  
    8. self.buttonBox.accepted.connect(self.accept)
    9. self.buttonBox.rejected.connect(self.reject)
    10.  
    11. self.addSettingCategories()
    12.  
    13.  
    14. def addSettingCategories(self):
    15. q = QTreeWidgetItem(self.settingsTree, ['General Settings'])
    16. q.setIcon(0, QIcon('icons/32x32/gear_in.png'))
    17.  
    18.  
    19. def accept(self):
    20. # Insert QSettings save code here
    21. print('accepted!')
    22. super(SettingsDialog, self).accept()
    To copy to clipboard, switch view to plain text mode 

    That works, it prints out "accepted!" two times (button pressed and released?). My question is a bit general: Is this a suitable approach, or how do you usually do this? I guess another options is to return the QSettings object to the MainWindow and save it to file there?
    Last edited by ecce; 1st May 2016 at 10:35.

Similar Threads

  1. Where to save QSettings?
    By arcull in forum Newbie
    Replies: 5
    Last Post: 23rd July 2015, 17:43
  2. do something while a button is pressed
    By saman_artorious in forum Qt Programming
    Replies: 11
    Last Post: 12th November 2013, 15:39
  3. Replies: 6
    Last Post: 4th October 2010, 03:19
  4. Getting the row for button pressed
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2007, 15:45
  5. QSettings does not save...
    By mtrpoland in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2007, 12:15

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.