How to Use QSettings: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
m (→‎mainwindow.h: pointer syntax wrong)
(12 intermediate revisions by 7 users not shown)
Line 1: Line 1:
[[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]
{{LangSwitch}}
[[Category:Developing with Qt::General]]
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Tutorial]]
{{DocLink|QSettings}} 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.


[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
Method {{DocLink|qsettings|setValue}} should be used to save a value of a key (aka the name of the setting).


'''English''' [[How_to_Use_QSettings_Bulgarian|Български]]
The value of a setting can be obtained using the method {{DocLink|qsettings|value}}.


= How to Use QSettings =
== Example ==
 
=== mainwindow.h ===
 
<code>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H


&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.
#include <QMainWindow>


== Save Settings ==
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>


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).
namespace Ui {
  class MainWindow;
}


== Load Settings ==
class MainWindow : public QMainWindow
{
Q_OBJECT
public:


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.
explicit MainWindow(QWidget *parent = 0);
virtual ~MainWindow();


== Example ==
private:
 
void initGui();


=== mainwindow.h ===
void loadSettings();


<code><br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H
void saveSettings();


#include &lt;QtGui/QMainWindow&amp;gt;
private slots:


#include &lt;QLabel&amp;gt;<br />#include &lt;QLineEdit&amp;gt;<br />#include &lt;QPushButton&amp;gt;
void handleButton();


namespace Ui {<br /> class MainWindow;<br />}
private:


class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT<br />public:
QString m_sSettingsFile;


explicit MainWindow(QWidget '''parent = 0);<br /> virtual ~MainWindow();
QLabel* m_pLabel;
<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;
QLineEdit* m_pEdit;


QPushButton* m_pButton;<br />};
QPushButton* m_pButton;
};


#endif // MAINWINDOW_H
#endif // MAINWINDOW_H
</code>
</code>


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


<code><br />#include &quot;mainwindow.h&amp;quot;
<code>
#include "mainwindow.h"
#include <QCoreApplication>
#include <QSettings>
#include <QApplication>


#include &lt;QtCore/QCoreApplication&amp;gt;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
m_pLabel(NULL),
m_pEdit(NULL),
m_pButton(NULL)
{
initGui();


#include &lt;QSettings&amp;gt;<br />#include &lt;QApplication&amp;gt;
m_sSettingsFile = QApplication::applicationDirPath().left(1) + ":/demosettings.ini";
loadSettings();


MainWindow::MainWindow(QWidget *parent)<br /> : QMainWindow(parent),<br /> m_pLabel(NULL),<br /> m_pEdit(NULL),<br /> m_pButton(NULL)<br />{<br /> initGui();
if (m_pButton)
{
  connect(m_pButton, SIGNAL (released()),this, SLOT (handleButton()));
}
}


m_sSettingsFile = QApplication::applicationDirPath().left(1) + &quot;:/demosettings.ini&amp;quot;;<br /> loadSettings();
void MainWindow::initGui()
{
m_pLabel = new QLabel("", this);
m_pLabel->setGeometry(0,0, 200,40);
m_pEdit = new QLineEdit("", this);
m_pEdit->setGeometry(0,40, 200,40);
m_pButton = new QPushButton("OK", this);
m_pButton->setGeometry(0,80, 200,40);
}


if (m_pButton)<br /> {<br /> connect(m_pButton, SIGNAL (released()),this, SLOT (handleButton()));<br /> }<br />}
void MainWindow::loadSettings()
{
QSettings settings(m_sSettingsFile, QSettings::NativeFormat);
QString sText = settings.value("text", "").toString();
if (m_pLabel)
{
  m_pLabel->setText(sText);
}
}


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::saveSettings()
{
QSettings settings(m_sSettingsFile, QSettings::NativeFormat);
QString sText = (m_pEdit) ? m_pEdit->text() : "";
settings.setValue("text", sText);
if (m_pLabel)
{
  m_pLabel->setText(sText);
}
}


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::handleButton()
{
saveSettings();
}


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 />}
MainWindow::~MainWindow()
{


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


=== main.cpp ===
=== main.cpp ===


<code><br />#include &quot;mainwindow.h&amp;quot;
<code>
#include "mainwindow.h"


#include &lt;QtGui/QApplication&amp;gt;
#include <QApplication>


int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv);
int main(int argc, char *argv[])
{
QApplication app(argc, argv);


MainWindow mainWindow;<br /> mainWindow.showMaximized();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}
MainWindow mainWindow;
mainWindow.showMaximized();
return app.exec();
}
</code>

Revision as of 17:12, 17 May 2017

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

QSettings 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.

Method setValue should be used to save a value of a key (aka the name of the setting).

The value of a setting can be obtained using the method value.

Example

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

namespace Ui {
  class MainWindow;
}

class MainWindow : public QMainWindow
{
 Q_OBJECT
public:

 explicit MainWindow(QWidget *parent = 0);
 virtual ~MainWindow();

private:

 void initGui();

 void loadSettings();

 void saveSettings();

private slots:

 void handleButton();

private:

 QString m_sSettingsFile;

 QLabel* m_pLabel;

 QLineEdit* m_pEdit;

 QPushButton* m_pButton;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include <QCoreApplication>
#include <QSettings>
#include <QApplication>

MainWindow::MainWindow(QWidget *parent)
 : QMainWindow(parent),
 m_pLabel(NULL),
 m_pEdit(NULL),
 m_pButton(NULL)
{
 initGui();

 m_sSettingsFile = QApplication::applicationDirPath().left(1) + ":/demosettings.ini";
 loadSettings();

if (m_pButton)
 {
  connect(m_pButton, SIGNAL (released()),this, SLOT (handleButton()));
 }
}

void MainWindow::initGui()
{
 m_pLabel = new QLabel("", this);
 m_pLabel->setGeometry(0,0, 200,40);
 m_pEdit = new QLineEdit("", this);
 m_pEdit->setGeometry(0,40, 200,40);
 m_pButton = new QPushButton("OK", this);
 m_pButton->setGeometry(0,80, 200,40);
}

void MainWindow::loadSettings()
{
 QSettings settings(m_sSettingsFile, QSettings::NativeFormat);
 QString sText = settings.value("text", "").toString();
 if (m_pLabel)
 {
  m_pLabel->setText(sText);
 }
}

void MainWindow::saveSettings()
{
 QSettings settings(m_sSettingsFile, QSettings::NativeFormat);
 QString sText = (m_pEdit) ? m_pEdit->text() : "";
 settings.setValue("text", sText);
 if (m_pLabel)
 {
  m_pLabel->setText(sText);
 }
}

void MainWindow::handleButton()
{
 saveSettings();
}

MainWindow::~MainWindow()
{

}

main.cpp

#include "mainwindow.h"

#include <QApplication>

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

 MainWindow mainWindow;
 mainWindow.showMaximized();
 return app.exec();
}