QSettings类是Qt中跨平台的配置信息类,在Application关闭时可以保存必要的数据信息;在程序启动时读取数据,恢复用户设置。在此,做一个小例子来测试其基本使用。
QSettingsDialog即录入用户数据,并且处理数据。
ui文件:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>QSettingsDialog</class> <widget class="QDialog" name="QSettingsDialog"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>315</width> <height>111</height> </rect> </property> <property name="sizePolicy"> <sizepolicy hsizetype="Fixed" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="contextMenuPolicy"> <enum>Qt::NoContextMenu</enum> </property> <property name="windowTitle"> <string>QSettingsDialog</string> </property> <widget class="QWidget" name="layoutWidget"> <property name="geometry"> <rect> <x>10</x> <y>10</y> <width>294</width> <height>85</height> </rect> </property> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <layout class="QVBoxLayout" name="verticalLayout"> <item> <layout class="QGridLayout" name="gridLayout_2"> <item row="0" column="0"> <widget class="QLabel" name="mLabelName"> <property name="text"> <string>Name</string> </property> </widget> </item> <item row="0" column="1" colspan="2"> <widget class="QLineEdit" name="mEditName"/> </item> <item row="1" column="0" colspan="2"> <widget class="QLabel" name="mLabelPhoneNum"> <property name="text"> <string>PhoneNumber</string> </property> </widget> </item> <item row="1" column="2"> <widget class="QLineEdit" name="mEditPhoneNum"/> </item> </layout> </item> <item> <spacer name="verticalSpacer"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>13</height> </size> </property> </spacer> </item> </layout> </item> <item> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <widget class="QPushButton" name="mBtnReadData"> <property name="text"> <string>ReadData</string> </property> </widget> </item> <item row="1" column="0"> <widget class="QPushButton" name="mBtnClear"> <property name="text"> <string>Clear</string> </property> </widget> </item> <item row="2" column="0"> <widget class="QPushButton" name="mBtnExit"> <property name="text"> <string>Exit</string> </property> </widget> </item> </layout> </item> </layout> </widget> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
具体显示如下:
qsettingsdialog.h文件:
#ifndef QSETTINGSDIALOG_H #define QSETTINGSDIALOG_H #include <QDialog> namespace Ui { class QSettingsDialog; } class QSettingsDialog : public QDialog { Q_OBJECT public: explicit QSettingsDialog(QWidget *parent = 0); ~QSettingsDialog(); private: //创建信号槽链接 void setupConnect(); void clearData(); //将数据写入QSettings void writeToSettings(); //从QSettings中读取数据 void readFromSettings(); private: Ui::QSettingsDialog *ui; const static QString BEGINSTRING; }; #endif // QSETTINGSDIALOG_H
qsettingsdialog.cpp文件:
#include "qsettingsdialog.h" #include "ui_qsettingsdialog.h" #include <QSettings> const QString QSettingsDialog::BEGINSTRING("part 1"); QSettingsDialog::QSettingsDialog(QWidget *parent) : QDialog(parent), ui(new Ui::QSettingsDialog) { ui->setupUi(this); setupConnect(); readFromSettings(); } QSettingsDialog::~QSettingsDialog() { delete ui; } void QSettingsDialog::setupConnect() { connect(ui->mBtnReadData, &QPushButton::clicked, this, &QSettingsDialog::writeToSettings); connect(ui->mBtnClear, &QPushButton::clicked, this, &QSettingsDialog::clearData); connect(ui->mBtnExit, &QPushButton::clicked, qApp, &QApplication::closeAllWindows); } void QSettingsDialog::clearData() { ui->mEditName->clear(); ui->mEditPhoneNum->clear(); writeToSettings(); } void QSettingsDialog::writeToSettings() { QSettings settings("Jowei Inc.", "TestSettings", this); settings.beginGroup(BEGINSTRING); settings.setValue("name", ui->mEditName->text().trimmed()); settings.setValue("phoneNumber", ui->mEditPhoneNum->text().trimmed()); settings.endGroup(); } void QSettingsDialog::readFromSettings() { QSettings settings("Jowei Inc.", "TestSettings", this); settings.beginGroup(BEGINSTRING); ui->mEditName->setText(settings.value("name").toString().trimmed()); ui->mEditPhoneNum->setText(settings.value("phoneNumber").toString().trimmed()); settings.endGroup(); }
当用户点击ReadData按钮时,将文本框中的数据写入到QSettings中。
当用户启动程序时,程序自动从QSettings读取信息,并写入到文本框中
时间: 2024-10-15 20:46:14