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)
(11 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{LangSwitch}}
[[Category:Developing with Qt::General]]
[[Category:Developing with Qt::General]]
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Snippets]]
[[Category:Tutorial]]
[[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="yes" depth="3"]
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 =
 
"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 ==
== Example ==
Line 28: Line 18:
#define MAINWINDOW_H
#define MAINWINDOW_H


#include <QtGui/QMainWindow>
#include <QMainWindow>


#include <QLabel>
#include <QLabel>
Line 35: Line 25:


namespace Ui {
namespace Ui {
class MainWindow;
  class MainWindow;
}
}


Line 43: Line 33:
public:
public:


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


Line 62: Line 52:
  QString m_sSettingsFile;
  QString m_sSettingsFile;


  QLabel''' m_pLabel;
  QLabel* m_pLabel;


QLineEdit* m_pEdit;
QLineEdit* m_pEdit;


QPushButton* m_pButton;
QPushButton* m_pButton;
};
};


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


Line 77: Line 66:
<code>
<code>
#include "mainwindow.h"
#include "mainwindow.h"
 
#include <QCoreApplication>
#include <QtCore/QCoreApplication>
 
#include <QSettings>
#include <QSettings>
#include <QApplication>
#include <QApplication>
Line 91: Line 78:
  initGui();
  initGui();


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


if (m_pButton)
if (m_pButton)
  {
  {
connect(m_pButton, SIGNAL (released()),this, SLOT (handleButton()));
  connect(m_pButton, SIGNAL (released()),this, SLOT (handleButton()));
  }
  }
}
}
Line 116: Line 103:
  if (m_pLabel)
  if (m_pLabel)
  {
  {
m_pLabel->setText(sText);
  m_pLabel->setText(sText);
  }
  }
}
}
Line 127: Line 114:
  if (m_pLabel)
  if (m_pLabel)
  {
  {
m_pLabel->setText(sText);
  m_pLabel->setText(sText);
  }
  }
}
}
Line 147: Line 134:
#include "mainwindow.h"
#include "mainwindow.h"


#include <QtGui/QApplication>
#include <QApplication>


int main(int argc, char *argv[])
int main(int argc, char *argv[])
Line 153: Line 140:
  QApplication app(argc, argv);
  QApplication app(argc, argv);


MainWindow mainWindow;
MainWindow mainWindow;
  mainWindow.showMaximized();
  mainWindow.showMaximized();
  return app.exec();
  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();
}