English Български

How to Use QSettings

QSettings [qt-project.org] class provides platform-independent application settings. The following example shows how to save and load settings to an ini file. The instance of QSettings will take care of writing and reading the settings from the ini file. The example has been tested on Nokia device with Symbian^3 OS.

Save Settings

Method setValue [qt-project.org] should be used to save a value of a key (aka the name of the setting).

Load Settings

The value of a setting can be obtained using method value [qt-project.org].

Example

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QtGui/QMainWindow>
  5.  
  6. #include <QLabel>
  7. #include <QLineEdit>
  8. #include <QPushButton>
  9.  
  10. namespace Ui {
  11.     class MainWindow;
  12. }
  13.  
  14. class MainWindow : public QMainWindow
  15. {
  16.     Q_OBJECT
  17. public:
  18.  
  19.     explicit MainWindow(QWidget *parent = 0);
  20.     virtual ~MainWindow();
  21.  
  22. private:
  23.  
  24.     void initGui();
  25.  
  26.     void loadSettings();
  27.  
  28.     void saveSettings();
  29.  
  30. private slots:
  31.  
  32.     void handleButton();
  33.  
  34. private:
  35.  
  36.     QString m_sSettingsFile;
  37.  
  38.     QLabel* m_pLabel;
  39.  
  40.     QLineEdit* m_pEdit;
  41.  
  42.     QPushButton* m_pButton;
  43. };
  44.  
  45. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2.  
  3. #include <QtCore/QCoreApplication>
  4.  
  5. #include <QSettings>
  6. #include <QApplication>
  7.  
  8. MainWindow::MainWindow(QWidget *parent)
  9.     : QMainWindow(parent),
  10.       m_pLabel(NULL),
  11.       m_pEdit(NULL),
  12.       m_pButton(NULL)
  13. {
  14.     initGui();
  15.  
  16.     m_sSettingsFile = QApplication::applicationDirPath().left(1) + ":/demosettings.ini";
  17.     loadSettings();
  18.  
  19.     if (m_pButton)
  20.     {
  21.         connect(m_pButton, SIGNAL(released()),this, SLOT(handleButton()));
  22.     }
  23. }
  24.  
  25. void MainWindow::initGui()
  26. {
  27.     m_pLabel = new QLabel("", this);
  28.     m_pLabel->setGeometry(0,0, 200,40);
  29.     m_pEdit = new QLineEdit("", this);
  30.     m_pEdit->setGeometry(0,40, 200,40);
  31.     m_pButton = new QPushButton("OK", this);
  32.     m_pButton->setGeometry(0,80, 200,40);
  33. }
  34.  
  35. void MainWindow::loadSettings()
  36. {
  37.     QSettings settings(m_sSettingsFile, QSettings::NativeFormat);
  38.     QString sText = settings.value("text", "").toString();
  39.     if (m_pLabel)
  40.     {
  41.         m_pLabel->setText(sText);
  42.     }
  43. }
  44.  
  45. void MainWindow::saveSettings()
  46. {
  47.     QSettings settings(m_sSettingsFile, QSettings::NativeFormat);
  48.     QString sText = (m_pEdit) ? m_pEdit->text() : "";
  49.     settings.setValue("text", sText);
  50.     if (m_pLabel)
  51.     {
  52.         m_pLabel->setText(sText);
  53.     }
  54. }
  55.  
  56. void MainWindow::handleButton()
  57. {
  58.     saveSettings();
  59. }
  60.  
  61. MainWindow::~MainWindow()
  62. {
  63.  
  64. }

main.cpp

  1. #include "mainwindow.h"
  2.  
  3. #include <QtGui/QApplication>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     QApplication app(argc, argv);
  8.  
  9.     MainWindow mainWindow;
  10.     mainWindow.showMaximized();
  11.     return app.exec();
  12. }

Categories: