实际的运用过程中,常需要应用程序能保存程序的状态以及用户的设置,如应用程序显示的大小,位置,背景颜色或用户设置的参数等信息,以便在下次运行程序时,能保持上次关闭的状态。
在Qt的使用过程中,我们使用Qsetting来保存程序的状态。使得当用户再次打开时,窗体仍然可以呈现半闭时的状态。
在Qt的使用过程中,我们使用Qsetting来保存程序的状态。使得当用户再次打开时,窗体仍然可以呈现半闭时的状态。
下面我举一个简单的例子加以说明:
贴上代码:
setting.h:
#ifndef SETTINGS_H
#define SETTINGS_H
#include <QDialog>
class QPushButton;
class QLabel;
class QTextEdit;
class Settings : public QDialog
{
Q_OBJECT
public:
Settings();
QLabel *label;
QPushButton *colorBtn;
QTextEdit *edit;
void readSettings();
void writeSettings();
protected:
void closeEvent(QCloseEvent *);
public slots:
void slotColor();
};
#endif // SETTINGS_H
setting.cpp:
#include "settings.h"
#include <QtGui>
Settings::Settings()
{
setWindowTitle("Settings");
label = new QLabel;
label->setText("Setting Test");
colorBtn = new QPushButton;
colorBtn->setText("Select Color");
edit = new QTextEdit;
QGridLayout *layout = new QGridLayout(this);
layout->addWidget(label,0,0);
layout->addWidget(colorBtn,0,1);
layout->addWidget(edit,1,0,1,2);
readSettings();
connect(colorBtn, SIGNAL(clicked()), this, SLOT(slotColor()));
}
void
Settings::slotColor()
{
QColor color = QColorDialog::getColor (Qt::blue);
if(color.isValid())
{
QPalette p = label->palette();
p.setColor(QPalette::Normal,QPalette::WindowText,color);
label->setPalette(p);
}
}
void
Settings::readSettings()
{
QSettings setting("MyPro","settings");
setting.beginGroup("Dialog");
QPoint pos = setting.value("position").toPoint();
QSize size = setting.value("size").toSize();
setting.endGroup();
setting.beginGroup("Content");
QColor color = setting.value("color").value<QColor>();
QString text = setting.value("text").toString();
setting.endGroup();
move(pos);
resize(size);
QPalette p = label->palette();
p.setColor(QPalette::Normal,QPalette::WindowText,color);
label->setPalette(p);
edit->setPlainText(text);
}
void
Settings::writeSettings()
{
QSettings setting("MyPro","settings");
setting.beginGroup("Dialog");
setting.setValue("position",pos());
setting.setValue("size",size());
setting.endGroup();
setting.beginGroup("Content");
setting.setValue("color",label->palette().color(QPalette::WindowText));
setting.setValue("text",edit->toPlainText());
setting.endGroup();
}
void
Settings::closeEvent(QCloseEvent *e)
{
writeSettings();
}
settting.pro:
######################################################################
# Automatically generated by qmake (2.01a) ?? ?? 10 23:13:13 2008
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += settings.h
SOURCES += main.cpp settings.cpp
# Automatically generated by qmake (2.01a) ?? ?? 10 23:13:13 2008
######################################################################
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += settings.h
SOURCES += main.cpp settings.cpp
main.cpp:
#include <QApplication>
#include "settings.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
Settings *s = new Settings();
s->show();
return app.exec();
}
#include "settings.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
Settings *s = new Settings();
s->show();
return app.exec();
}
运行效果如下:
点击"select color"按钮,选择一种颜色,
如下:
![](https://i-blog.csdnimg.cn/blog_migrate/4bb7c8da3a541727727f7184f3ee1ad1.gif)
再次打开,还是显示关闭时的状态。
转载于:https://blog.51cto.com/prothesman/1411690