How to Use QSettings/bg: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
[[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]
[[Category:Developing with Qt::General]]
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Tutorial]]


[toc align_right="yes" depth="3"]
[toc align_right="yes" depth="3"]
Line 21: Line 24:
=== mainwindow.h ===
=== mainwindow.h ===


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


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


#include <QLabel><br />#include <QLineEdit><br />#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>


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


class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT<br />public:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:


explicit MainWindow(QWidget '''parent = 0);<br /> virtual ~MainWindow();
explicit MainWindow(QWidget '''parent = 0);
<br />private:
virtual ~MainWindow();
<br /> void initGui();
 
<br /> void loadSettings();
private:
<br /> void saveSettings();
 
<br />private slots:
void initGui();
<br /> void handleButton();
 
<br />private:
void loadSettings();
<br /> QString m_sSettingsFile;
 
<br /> QLabel''' m_pLabel;
void saveSettings();
 
private slots:
 
void handleButton();
 
private:
 
QString m_sSettingsFile;
 
QLabel''' m_pLabel;


QLineEdit* m_pEdit;
QLineEdit* m_pEdit;


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


#endif // MAINWINDOW_H
#endif // MAINWINDOW_H
Line 52: Line 75:
=== mainwindow.cpp ===
=== mainwindow.cpp ===


<code><br />#include "mainwindow.h"
<code>
#include "mainwindow.h"


#include <QtCore/QCoreApplication>
#include <QtCore/QCoreApplication>


#include <QSettings><br />#include <QApplication>
#include <QSettings>
#include <QApplication>


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


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


if (m_pButton)<br /> {<br /> connect(m_pButton, SIGNAL (released()),this, SLOT (handleButton()));<br /> }<br />}
if (m_pButton)
{
connect(m_pButton, SIGNAL (released()),this, SLOT (handleButton()));
}
}


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", this);<br /> m_pButton->setGeometry(0,80, 200,40);<br />}
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()<br />{<br /> QSettings settings(m_sSettingsFile, QSettings::NativeFormat);<br /> QString sText = settings.value("text", "").toString();<br /> if (m_pLabel)<br /> {<br /> m_pLabel->setText(sText);<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::saveSettings()<br />{<br /> QSettings settings(m_sSettingsFile, QSettings::NativeFormat);<br /> QString sText = (m_pEdit) ? m_pEdit->text() : "";<br /> settings.setValue("text", sText);<br /> if (m_pLabel)<br /> {<br /> m_pLabel->setText(sText);<br /> }<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::handleButton()<br />{<br /> saveSettings();<br />}
void MainWindow::handleButton()
{
saveSettings();
}


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


}<br /></code>
}
</code>


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


<code><br />#include "mainwindow.h"
<code>
#include "mainwindow.h"


#include <QtGui/QApplication>
#include <QtGui/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();<br />}
MainWindow mainWindow;
mainWindow.showMaximized();
return app.exec();
}

Revision as of 12:14, 25 February 2015


[toc align_right="yes" depth="3"]

Български English

Как се използва QSettings

Класът "QSettings":http://doc.qt.io/qt-5.0/qtcore/qsettings.html осигурява платформено-независими настройки за приложението. Приложеният пример показва как да се записват и зареждат настройки в ini файл. Инстанцията на QSettings ще се погрижи за записа и четенето на настройките в ini файла. Примерът е тестван на Nokia устройство с ОС Symbian^3.

Запис на настройки

Методът "setValue":http://doc.qt.io/qt-5.0/qtcore/qsettings.html#setValue трябва да бъде използван, за да се запамени стойност към ключ (т.е. към името на насройката).

Зареждане на настройки

Стойността на дадена настройка може да бъде взета чрез метода "value":http://doc.qt.io/qt-5.0/qtcore/qsettings.html#value.

Пример

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/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 <QtCore/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

  1. include "mainwindow.h"
  1. include <QtGui/QApplication>

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

QApplication app(argc, argv);

MainWindow mainWindow;

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

}