Table of Content
English Български
How to Use QSettings
QSettings [doc.qt.nokia.com] 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 [doc.qt.nokia.com] 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 [doc.qt.nokia.com].
Example
mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QtGui/QMainWindow>
- #include <QLabel>
- #include <QLineEdit>
- #include <QPushButton>
- namespace Ui {
- class MainWindow;
- }
- {
- Q_OBJECT
- public:
- virtual ~MainWindow();
- private:
- void initGui();
- void loadSettings();
- void saveSettings();
- private slots:
- void handleButton();
- private:
- QString m_sSettingsFile;
- };
- #endif // MAINWINDOW_H
mainwindow.cpp
- #include "mainwindow.h"
- #include <QtCore/QCoreApplication>
- #include <QSettings>
- #include <QApplication>
- m_pLabel(NULL),
- m_pEdit(NULL),
- m_pButton(NULL)
- {
- initGui();
- loadSettings();
- if (m_pButton)
- {
- connect(m_pButton, SIGNAL(released()),this, SLOT(handleButton()));
- }
- }
- void MainWindow::initGui()
- {
- m_pLabel->setGeometry(0,0, 200,40);
- m_pEdit->setGeometry(0,40, 200,40);
- m_pButton->setGeometry(0,80, 200,40);
- }
- void MainWindow::loadSettings()
- {
- if (m_pLabel)
- {
- m_pLabel->setText(sText);
- }
- }
- void MainWindow::saveSettings()
- {
- settings.setValue("text", sText);
- if (m_pLabel)
- {
- m_pLabel->setText(sText);
- }
- }
- void MainWindow::handleButton()
- {
- saveSettings();
- }
- MainWindow::~MainWindow()
- {
- }
main.cpp
- #include "mainwindow.h"
- #include <QtGui/QApplication>
- int main(int argc, char *argv[])
- {
- MainWindow mainWindow;
- mainWindow.showMaximized();
- return app.exec();
- }

