Results 1 to 3 of 3

Thread: Save QSettings when OK button is pressed

  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 11:35.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Save QSettings when OK button is pressed

    The only weird things is that you see the output twice, but otherwise that sounds good.

    Overwriting accept() also allows you to do some final check on the value and, if necessary, keep the dialog open by just not calling the super class accept.

    The only thing you probably need to do additionally is to make sure that other parts of the application which need those settings read them again.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Save QSettings when OK button is pressed

    The only weird things is that you see the output twice, but otherwise that sounds good.
    @ecce: Did you possibly make a connection in your ui code when you created the dialog in Qt Designer? That's the only thing I can think of that would cause the slot to be entered twice.

Similar Threads

  1. Where to save QSettings?
    By arcull in forum Newbie
    Replies: 5
    Last Post: 23rd July 2015, 18:43
  2. do something while a button is pressed
    By saman_artorious in forum Qt Programming
    Replies: 11
    Last Post: 12th November 2013, 16:39
  3. Replies: 6
    Last Post: 4th October 2010, 04:19
  4. Getting the row for button pressed
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 28th November 2007, 16:45
  5. QSettings does not save...
    By mtrpoland in forum Qt Programming
    Replies: 2
    Last Post: 28th August 2007, 13: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.