How to Use QSettings: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[How to Use QSettings Bulgarian|Български]]
[[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]


=How to Use QSettings=
[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]


[http://doc.qt.io/qt-5.0/qtcore/qsettings.html QSettings] ''[qt.io]'' 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.
'''English''' [[How_to_Use_QSettings_Bulgarian|Български]]


==Save Settings==
= How to Use QSettings =


Method [http://doc.qt.io/qt-5.0/qtcore/qsettings.html#setValue setValue] ''[qt.io]'' should be used to save a value of a key (aka the name of the setting).
&quot;QSettings&amp;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.


==Load Settings==
== Save Settings ==


The value of a setting can be obtained using method [http://doc.qt.io/qt-5.0/qtcore/qsettings.html#value value] ''[qt.io]''.
Method &quot;setValue&amp;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).


==Example==
== Load Settings ==


===mainwindow.h===
The value of a setting can be obtained using method &quot;value&amp;quot;:http://doc.qt.io/qt-5.0/qtcore/qsettings.html#value.


===mainwindow.cpp===
== Example ==


===main.cpp===
=== mainwindow.h ===


===Categories:===
<code><br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H


* [[:Category:Developing-with-Qt|Developing with Qt]]
#include &lt;QtGui/QMainWindow&amp;gt;
** [[:Category:Developing-with-Qt::General|General]]
 
* [[:Category:HowTo|HowTo]]
#include &lt;QLabel&amp;gt;<br />#include &lt;QLineEdit&amp;gt;<br />#include &lt;QPushButton&amp;gt;
* [[:Category:snippets|snippets]]
 
* [[:Category:Tutorial|Tutorial]]
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 &quot;mainwindow.h&amp;quot;
 
#include &lt;QtCore/QCoreApplication&amp;gt;
 
#include &lt;QSettings&amp;gt;<br />#include &lt;QApplication&amp;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) + &quot;:/demosettings.ini&amp;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(&quot;&quot;, this);<br /> m_pLabel-&gt;setGeometry(0,0, 200,40);<br /> m_pEdit = new QLineEdit(&quot;&quot;, this);<br /> m_pEdit-&gt;setGeometry(0,40, 200,40);<br /> m_pButton = new QPushButton(&quot;OK&amp;quot;, this);<br /> m_pButton-&gt;setGeometry(0,80, 200,40);<br />}
 
void MainWindow::loadSettings()<br />{<br /> QSettings settings(m_sSettingsFile, QSettings::NativeFormat);<br /> QString sText = settings.value(&quot;text&amp;quot;, &quot;&quot;).toString();<br /> if (m_pLabel)<br /> {<br /> m_pLabel-&gt;setText(sText);<br /> }<br />}
 
void MainWindow::saveSettings()<br />{<br /> QSettings settings(m_sSettingsFile, QSettings::NativeFormat);<br /> QString sText = (m_pEdit) ? m_pEdit-&gt;text() : &quot;&quot;;<br /> settings.setValue(&quot;text&amp;quot;, sText);<br /> if (m_pLabel)<br /> {<br /> m_pLabel-&gt;setText(sText);<br /> }<br />}
 
void MainWindow::handleButton()<br />{<br /> saveSettings();<br />}
 
MainWindow::~MainWindow()<br />{
 
}<br /></code>
 
=== main.cpp ===
 
<code><br />#include &quot;mainwindow.h&amp;quot;
 
#include &lt;QtGui/QApplication&amp;gt;
 
int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv);
 
MainWindow mainWindow;<br /> mainWindow.showMaximized();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}

Revision as of 14:16, 23 February 2015




[toc align_right="yes&quot; depth="3&quot;]

English Български

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

<br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H

#include &lt;QtGui/QMainWindow&amp;gt;

#include &lt;QLabel&amp;gt;<br />#include &lt;QLineEdit&amp;gt;<br />#include &lt;QPushButton&amp;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 &quot;mainwindow.h&amp;quot;

#include &lt;QtCore/QCoreApplication&amp;gt;

#include &lt;QSettings&amp;gt;<br />#include &lt;QApplication&amp;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) + &quot;:/demosettings.ini&amp;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(&quot;&quot;, this);<br /> m_pLabel-&gt;setGeometry(0,0, 200,40);<br /> m_pEdit = new QLineEdit(&quot;&quot;, this);<br /> m_pEdit-&gt;setGeometry(0,40, 200,40);<br /> m_pButton = new QPushButton(&quot;OK&amp;quot;, this);<br /> m_pButton-&gt;setGeometry(0,80, 200,40);<br />}

void MainWindow::loadSettings()<br />{<br /> QSettings settings(m_sSettingsFile, QSettings::NativeFormat);<br /> QString sText = settings.value(&quot;text&amp;quot;, &quot;&quot;).toString();<br /> if (m_pLabel)<br /> {<br /> m_pLabel-&gt;setText(sText);<br /> }<br />}

void MainWindow::saveSettings()<br />{<br /> QSettings settings(m_sSettingsFile, QSettings::NativeFormat);<br /> QString sText = (m_pEdit) ? m_pEdit-&gt;text() : &quot;&quot;;<br /> settings.setValue(&quot;text&amp;quot;, sText);<br /> if (m_pLabel)<br /> {<br /> m_pLabel-&gt;setText(sText);<br /> }<br />}

void MainWindow::handleButton()<br />{<br /> saveSettings();<br />}

MainWindow::~MainWindow()<br />{

}<br />

main.cpp


#include "mainwindow.h&quot;

  1. include <QtGui/QApplication&gt;

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

MainWindow mainWindow;
mainWindow.showMaximized();
return app.exec&amp;#40;&#41;;
}