How to Use QSettings: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]] | |||
= | [toc align_right="yes&quot; depth="3&quot;] | ||
'''English''' [[How_to_Use_QSettings_Bulgarian|Български]] | |||
= | = How to Use QSettings = | ||
"QSettings&quot;:http://doc.qt.io/qt-5.0/qtcore/qsettings.html 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&quot;:http://doc.qt.io/qt-5.0/qtcore/qsettings.html#setValue 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&quot;:http://doc.qt.io/qt-5.0/qtcore/qsettings.html#value. | |||
== | == Example == | ||
=== | === mainwindow.h === | ||
<code><br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H | |||
* | #include <QtGui/QMainWindow&gt; | ||
#include <QLabel&gt;<br />#include <QLineEdit&gt;<br />#include <QPushButton&gt; | |||
* [ | namespace Ui {<br /> class MainWindow;<br />} | ||
class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT<br />public: | |||
explicit MainWindow(QWidget '''parent = 0);<br /> virtual ~MainWindow(); | |||
<br />private: | |||
<br /> void initGui(); | |||
<br /> void loadSettings(); | |||
<br /> void saveSettings(); | |||
<br />private slots: | |||
<br /> void handleButton(); | |||
<br />private: | |||
<br /> QString m_sSettingsFile; | |||
<br /> QLabel''' m_pLabel; | |||
QLineEdit* m_pEdit; | |||
QPushButton* m_pButton;<br />}; | |||
#endif // MAINWINDOW_H | |||
</code> | |||
=== mainwindow.cpp === | |||
<code><br />#include "mainwindow.h&quot; | |||
#include <QtCore/QCoreApplication&gt; | |||
#include <QSettings&gt;<br />#include <QApplication&gt; | |||
MainWindow::MainWindow(QWidget *parent)<br /> : QMainWindow(parent),<br /> m_pLabel(NULL),<br /> m_pEdit(NULL),<br /> m_pButton(NULL)<br />{<br /> initGui(); | |||
m_sSettingsFile = QApplication::applicationDirPath().left(1) + ":/demosettings.ini&quot;;<br /> loadSettings(); | |||
if (m_pButton)<br /> {<br /> connect(m_pButton, SIGNAL (released()),this, SLOT (handleButton()));<br /> }<br />} | |||
void MainWindow::initGui()<br />{<br /> m_pLabel = new QLabel("", this);<br /> m_pLabel->setGeometry(0,0, 200,40);<br /> m_pEdit = new QLineEdit("", this);<br /> m_pEdit->setGeometry(0,40, 200,40);<br /> m_pButton = new QPushButton("OK&quot;, this);<br /> m_pButton->setGeometry(0,80, 200,40);<br />} | |||
void MainWindow::loadSettings()<br />{<br /> QSettings settings(m_sSettingsFile, QSettings::NativeFormat);<br /> QString sText = settings.value("text&quot;, "").toString();<br /> if (m_pLabel)<br /> {<br /> m_pLabel->setText(sText);<br /> }<br />} | |||
void MainWindow::saveSettings()<br />{<br /> QSettings settings(m_sSettingsFile, QSettings::NativeFormat);<br /> QString sText = (m_pEdit) ? m_pEdit->text() : "";<br /> settings.setValue("text&quot;, sText);<br /> if (m_pLabel)<br /> {<br /> m_pLabel->setText(sText);<br /> }<br />} | |||
void MainWindow::handleButton()<br />{<br /> saveSettings();<br />} | |||
MainWindow::~MainWindow()<br />{ | |||
}<br /></code> | |||
=== main.cpp === | |||
<code><br />#include "mainwindow.h&quot; | |||
#include <QtGui/QApplication&gt; | |||
int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv); | |||
MainWindow mainWindow;<br /> mainWindow.showMaximized();<br /> return app.exec&amp;#40;&#41;;<br />} |
Revision as of 14:16, 23 February 2015
[toc align_right="yes" depth="3"]
English Български
How to Use QSettings
"QSettings":http://doc.qt.io/qt-5.0/qtcore/qsettings.html 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":http://doc.qt.io/qt-5.0/qtcore/qsettings.html#setValue 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":http://doc.qt.io/qt-5.0/qtcore/qsettings.html#value.
Example
mainwindow.h
<br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H
#include <QtGui/QMainWindow&gt;
#include <QLabel&gt;<br />#include <QLineEdit&gt;<br />#include <QPushButton&gt;
namespace Ui {<br /> class MainWindow;<br />}
class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT<br />public:
explicit MainWindow(QWidget '''parent = 0);<br /> virtual ~MainWindow();
<br />private:
<br /> void initGui();
<br /> void loadSettings();
<br /> void saveSettings();
<br />private slots:
<br /> void handleButton();
<br />private:
<br /> QString m_sSettingsFile;
<br /> QLabel''' m_pLabel;
QLineEdit* m_pEdit;
QPushButton* m_pButton;<br />};
#endif // MAINWINDOW_H
mainwindow.cpp
<br />#include "mainwindow.h&quot;
#include <QtCore/QCoreApplication&gt;
#include <QSettings&gt;<br />#include <QApplication&gt;
MainWindow::MainWindow(QWidget *parent)<br /> : QMainWindow(parent),<br /> m_pLabel(NULL),<br /> m_pEdit(NULL),<br /> m_pButton(NULL)<br />{<br /> initGui();
m_sSettingsFile = QApplication::applicationDirPath().left(1) + ":/demosettings.ini&quot;;<br /> loadSettings();
if (m_pButton)<br /> {<br /> connect(m_pButton, SIGNAL (released()),this, SLOT (handleButton()));<br /> }<br />}
void MainWindow::initGui()<br />{<br /> m_pLabel = new QLabel("", this);<br /> m_pLabel->setGeometry(0,0, 200,40);<br /> m_pEdit = new QLineEdit("", this);<br /> m_pEdit->setGeometry(0,40, 200,40);<br /> m_pButton = new QPushButton("OK&quot;, this);<br /> m_pButton->setGeometry(0,80, 200,40);<br />}
void MainWindow::loadSettings()<br />{<br /> QSettings settings(m_sSettingsFile, QSettings::NativeFormat);<br /> QString sText = settings.value("text&quot;, "").toString();<br /> if (m_pLabel)<br /> {<br /> m_pLabel->setText(sText);<br /> }<br />}
void MainWindow::saveSettings()<br />{<br /> QSettings settings(m_sSettingsFile, QSettings::NativeFormat);<br /> QString sText = (m_pEdit) ? m_pEdit->text() : "";<br /> settings.setValue("text&quot;, sText);<br /> if (m_pLabel)<br /> {<br /> m_pLabel->setText(sText);<br /> }<br />}
void MainWindow::handleButton()<br />{<br /> saveSettings();<br />}
MainWindow::~MainWindow()<br />{
}<br />
main.cpp
#include "mainwindow.h"
- include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.showMaximized();
return app.exec&#40;);
}