How to Use QSettings: Difference between revisions
Jump to navigation
Jump to search
m (→Example: syntaxhighlight for qt framework instead of just c++) |
(Syntax highlight fix) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 13: | Line 13: | ||
=== mainwindow.h === | === mainwindow.h === | ||
<syntaxhighlight lang="c++"> | |||
<syntaxhighlight lang=" | |||
#ifndef MAINWINDOW_H | #ifndef MAINWINDOW_H | ||
#define MAINWINDOW_H | #define MAINWINDOW_H | ||
Line 33: | Line 32: | ||
public: | public: | ||
explicit MainWindow(QWidget | explicit MainWindow(QWidget *parent = 0); | ||
virtual ~MainWindow(); | virtual ~MainWindow(); | ||
Line 61: | Line 60: | ||
#endif // MAINWINDOW_H | #endif // MAINWINDOW_H | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== mainwindow.cpp === | === mainwindow.cpp === | ||
<syntaxhighlight lang="c++"> | |||
<syntaxhighlight lang=" | |||
#include "mainwindow.h" | #include "mainwindow.h" | ||
#include <QCoreApplication> | #include <QCoreApplication> | ||
Line 78: | Line 75: | ||
initGui(); | initGui(); | ||
m_sSettingsFile = QApplication::applicationDirPath( | m_sSettingsFile = QApplication::applicationDirPath() + "/demosettings.ini"; | ||
loadSettings(); | loadSettings(); | ||
Line 112: | Line 109: | ||
QString sText = (m_pEdit) ? m_pEdit->text() : ""; | QString sText = (m_pEdit) ? m_pEdit->text() : ""; | ||
settings.setValue("text", sText); | settings.setValue("text", sText); | ||
} | } | ||
Line 131: | Line 124: | ||
=== main.cpp === | === main.cpp === | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="c++"> | ||
#include "mainwindow.h" | #include "mainwindow.h" | ||
Latest revision as of 15:32, 2 May 2023
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() + "/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);
}
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();
}