现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

Qt让控件(widgets)内容可以自动 保存/读取 到配置文件(QSettings)中

2015-04-09 06:16 工业·编程 ⁄ 共 12234字 ⁄ 字号 暂无评论

有时候,我们会有很多很多的控件数据需要保存,比如说QLineEdit的text,或者说QSpinBox的value.手动去维护,太烦人.于是,我就整理了一下,让数据可以自动的保存和读取.

说明一下,我这里没有写完所有的控件,但是框架我已经给你搭好了,希望能给你启迪.

.h文件

#include <QWidget>

#include <QLineEdit>

#include <QTextEdit>

#include <QSpinBox>

#include <QDoubleSpinBox>

#include <QTimeEdit>

#include <QDateEdit>

#include <QDateTimeEdit>

#include <QDial>

#include <QSlider>

#include <QAbstractSlider>

#include <QLabel>

#include <QTextBrowser>

#include <QLCDNumber>

#include <QProgressBar>

#include <QRadioButton>

#include <QCheckBox>

class JasonQt_Settings_WidgetDataControl 

public: 

enum controlFlagsEnum 

    { 

        controlFlagsRecursion       = 0x1, 

        controlFlagsPos             = 0x2, 

        controlFlagsSize            = 0x4, 

        controlFlagsGeometry        = 0x6, 

        controlFlagsEnabled         = 0x8, 

        controlFlagsQLineEdit       =   0x10, 

        controlFlagsQTextEdit       =   0x20, 

        controlFlagsQSpinBox        =   0x40, 

        controlFlagsQDoubleSpinBox  =   0x80, 

        controlFlagsQTimeEdit       =  0x100, 

        controlFlagsQDateEdit       =  0x200, 

        controlFlagsQDateTimeEdit   =  0x400, 

        controlFlagsQDial           =  0x800, 

        controlFlagsQSlider         = 0x1000, 

        controlFlagsQAbstractSlider = 0x2000, 

        controlFlagsInputWidgets    = 0xfff0, 

        controlFlagsQLabel          = 0x10000, 

        controlFlagsQTextBrowser    = 0x20000, 

        controlFlagsQLCDNumber      = 0x40000, 

        controlFlagsQProgressBar    = 0x80000, 

        controlFlagsDisplayWidgets  = 0xf0000, 

        controlFlagsQRadioButton    = 0x1000000, 

        controlFlagsQCheckBox       = 0x2000000, 

        controlFlagsButtons         = 0xf000000, 

        controlFlagsAll             = 0xffffffff 

    }; 

public: 

static void save(QSettings *settings, const QWidget *widget, const controlFlagsEnum &controlFlags); 

static void read(QSettings *settings, QWidget *widget, const controlFlagsEnum &controlFlags); 

}; 

.cpp文件

void JasonQt_Settings_WidgetDataControl::save(QSettings *settings, const QWidget *widget, const controlFlagsEnum &controlFlags) 

if(widget->objectName().isEmpty()) { return; } 

    settings->beginGroup(widget->objectName()); 

const QObjectList children = widget->children(); 

    foreach(QObject *now, children) 

    { 

if(now->objectName().isEmpty()) { continue; } 

if(now->objectName().indexOf("qt_") == 0) { continue; } 

if((controlFlags & controlFlagsPos) && now->inherits("QWidget")) 

        { 

            settings->setValue(now->objectName() + ".pos", qobject_cast<QWidget *>(now)->pos()); 

        } 

if((controlFlags & controlFlagsSize) && now->inherits("QWidget")) 

        { 

            settings->setValue(now->objectName() + ".size", qobject_cast<QWidget *>(now)->size()); 

        } 

if((controlFlags & controlFlagsEnabled) && now->inherits("QWidget")) 

        { 

            settings->setValue(now->objectName() + ".enabled", qobject_cast<QWidget *>(now)->isEnabled()); 

        } 

if((controlFlags & controlFlagsQLineEdit) && now->inherits("QLineEdit")) 

        { 

            settings->setValue(now->objectName() + ".QLineEdit", qobject_cast<QLineEdit *>(now)->text()); 

continue; 

        } 

if((controlFlags & controlFlagsQTextEdit) && now->inherits("QTextEdit")) 

        { 

            settings->setValue(now->objectName() + ".QTextEdit", qobject_cast<QTextEdit *>(now)->toPlainText()); 

continue; 

        } 

if((controlFlags & controlFlagsQSpinBox) && now->inherits("QSpinBox")) 

        { 

            settings->setValue(now->objectName() + ".QSpinBox", qobject_cast<QSpinBox *>(now)->value()); 

continue; 

        } 

if((controlFlags & controlFlagsQDoubleSpinBox) && now->inherits("QDoubleSpinBox")) 

        { 

            settings->setValue(now->objectName() + ".QDoubleSpinBox", qobject_cast<QDoubleSpinBox *>(now)->value()); 

continue; 

        } 

if((controlFlags & controlFlagsQTimeEdit) && now->inherits("QTimeEdit")) 

        { 

            settings->setValue(now->objectName() + ".QTimeEdit", qobject_cast<QTimeEdit *>(now)->time()); 

continue; 

        } 

if((controlFlags & controlFlagsQDateEdit) && now->inherits("QDateEdit")) 

        { 

            settings->setValue(now->objectName() + ".QDateEdit", qobject_cast<QDateEdit *>(now)->date()); 

continue; 

        } 

if((controlFlags & controlFlagsQDateTimeEdit) && now->inherits("QDateTimeEdit")) 

        { 

            settings->setValue(now->objectName() + ".QDateTimeEdit", qobject_cast<QDateTimeEdit *>(now)->dateTime()); 

continue; 

        } 

if((controlFlags & controlFlagsQDial) && now->inherits("QDial")) 

        { 

            settings->setValue(now->objectName() + ".QDial", qobject_cast<QDial *>(now)->value()); 

continue; 

        } 

if((controlFlags & controlFlagsQSlider) && now->inherits("QSlider")) 

        { 

            settings->setValue(now->objectName() + ".QSlider", qobject_cast<QSlider *>(now)->value()); 

continue; 

        } 

if((controlFlags & controlFlagsQAbstractSlider) && now->inherits("QAbstractSlider")) 

        { 

            settings->setValue(now->objectName() + ".QAbstractSlider", qobject_cast<QAbstractSlider *>(now)->value()); 

continue; 

        } 

if((controlFlags & controlFlagsQLabel) && now->inherits("QLabel")) 

        { 

            settings->setValue(now->objectName() + ".QLabel", qobject_cast<QLabel *>(now)->text()); 

continue; 

        } 

if((controlFlags & controlFlagsQTextBrowser) && now->inherits("QTextBrowser")) 

        { 

            settings->setValue(now->objectName() + ".QTextBrowser", qobject_cast<QTextBrowser *>(now)->toPlainText()); 

continue; 

        } 

if((controlFlags & controlFlagsQLCDNumber) && now->inherits("QLCDNumber")) 

        { 

            settings->setValue(now->objectName() + ".QLCDNumber", qobject_cast<QLCDNumber *>(now)->value()); 

continue; 

        } 

if((controlFlags & controlFlagsQProgressBar) && now->inherits("QProgressBar")) 

        { 

            settings->setValue(now->objectName() + ".QProgressBar", qobject_cast<QProgressBar *>(now)->value()); 

continue; 

        } 

if((controlFlags & controlFlagsQRadioButton) && now->inherits("QRadioButton")) 

        { 

            settings->setValue(now->objectName() + ".QRadioButton", qobject_cast<QRadioButton *>(now)->isChecked()); 

continue; 

        } 

if((controlFlags & controlFlagsQCheckBox) && now->inherits("QCheckBox")) 

        { 

            settings->setValue(now->objectName() + ".QCheckBox", qobject_cast<QCheckBox *>(now)->isChecked()); 

continue; 

        } 

    } 

if(controlFlags & controlFlagsRecursion) 

    { 

        foreach(QObject *now, children) 

        { 

if(now->inherits("QWidget")) 

            { 

                save(settings, qobject_cast<QWidget *>(now), controlFlags); 

            } 

        } 

    } 

    settings->endGroup(); 

void JasonQt_Settings_WidgetDataControl::read(QSettings *settings, QWidget *widget, const controlFlagsEnum &controlFlags) 

if(widget->objectName().isEmpty()) { return; } 

    settings->beginGroup(widget->objectName()); 

const QObjectList children = widget->children(); 

    foreach(QObject *now, children) 

    { 

if(now->objectName().isEmpty()) { continue; } 

if(now->objectName().indexOf("qt_") == 0) { continue; } 

if((controlFlags & controlFlagsPos) && now->inherits("QWidget") && settings->contains(now->objectName() + ".pos")) 

        { 

            qobject_cast<QWidget *>(now)->move(settings->value(now->objectName() + ".pos").toPoint()); 

        } 

if((controlFlags & controlFlagsSize) && now->inherits("QWidget") && settings->contains(now->objectName() + ".size")) 

        { 

            qobject_cast<QWidget *>(now)->resize(settings->value(now->objectName() + ".size").toSize()); 

        } 

if((controlFlags & controlFlagsSize) && now->inherits("QWidget") && settings->contains(now->objectName() + ".enabled")) 

        { 

            qobject_cast<QWidget *>(now)->setEnabled(settings->value(now->objectName() + ".enabled").toBool()); 

        } 

if((controlFlags & controlFlagsQLineEdit) && now->inherits("QLineEdit") && settings->contains(now->objectName() + ".QLineEdit")) 

        { 

            qobject_cast<QLineEdit *>(now)->setText(settings->value(now->objectName() + ".QLineEdit").toString()); 

continue; 

        } 

if((controlFlags & controlFlagsQTextEdit) && now->inherits("QTextEdit") && settings->contains(now->objectName() + ".QTextEdit")) 

        { 

            qobject_cast<QTextEdit *>(now)->setPlainText(settings->value(now->objectName() + ".QTextEdit").toString()); 

continue; 

        } 

if((controlFlags & controlFlagsQSpinBox) && now->inherits("QSpinBox") && settings->contains(now->objectName() + ".QSpinBox")) 

        { 

            qobject_cast<QSpinBox *>(now)->setValue(settings->value(now->objectName() + ".QSpinBox").toInt()); 

continue; 

        } 

if((controlFlags & controlFlagsQDoubleSpinBox) && now->inherits("QDoubleSpinBox") && settings->contains(now->objectName() + ".QDoubleSpinBox")) 

        { 

            qobject_cast<QDoubleSpinBox *>(now)->setValue(settings->value(now->objectName() + ".QDoubleSpinBox").toDouble()); 

continue; 

        } 

if((controlFlags & controlFlagsQTimeEdit) && now->inherits("QTimeEdit") && settings->contains(now->objectName() + ".QTimeEdit")) 

        { 

            qobject_cast<QTimeEdit *>(now)->setTime(settings->value(now->objectName() + ".QTimeEdit").toTime()); 

continue; 

        } 

if((controlFlags & controlFlagsQDateEdit) && now->inherits("QDateEdit") && settings->contains(now->objectName() + ".QDateEdit")) 

        { 

            qobject_cast<QDateEdit *>(now)->setDate(settings->value(now->objectName() + ".QDateEdit").toDate()); 

continue; 

        } 

if((controlFlags & controlFlagsQDateTimeEdit) && now->inherits("QDateTimeEdit") && settings->contains(now->objectName() + ".QDateTimeEdit")) 

        { 

            qobject_cast<QDateTimeEdit *>(now)->setDateTime(settings->value(now->objectName() + ".QDateTimeEdit").toDateTime()); 

continue; 

        } 

if((controlFlags & controlFlagsQDial) && now->inherits("QDial") && settings->contains(now->objectName() + ".QDial")) 

        { 

            qobject_cast<QDial *>(now)->setValue(settings->value(now->objectName() + ".QDial").toInt()); 

continue; 

        } 

if((controlFlags & controlFlagsQSlider) && now->inherits("QSlider") && settings->contains(now->objectName() + ".QSlider")) 

        { 

            qobject_cast<QSlider *>(now)->setValue(settings->value(now->objectName() + ".QSlider").toInt()); 

continue; 

        } 

if((controlFlags & controlFlagsQAbstractSlider) && now->inherits("QAbstractSlider") && settings->contains(now->objectName() + ".QAbstractSlider")) 

        { 

            qobject_cast<QAbstractSlider *>(now)->setValue(settings->value(now->objectName() + ".QAbstractSlider").toInt()); 

continue; 

        } 

if((controlFlags & controlFlagsQLabel) && now->inherits("QLabel") && settings->contains(now->objectName() + ".QLabel")) 

        { 

            qobject_cast<QLabel *>(now)->setText(settings->value(now->objectName() + ".QLabel").toString()); 

continue; 

        } 

if((controlFlags & controlFlagsQTextBrowser) && now->inherits("QTextBrowser") && settings->contains(now->objectName() + ".QTextBrowser")) 

        { 

            qobject_cast<QTextBrowser *>(now)->setText(settings->value(now->objectName() + ".QTextBrowser").toString()); 

continue; 

        } 

if((controlFlags & controlFlagsQLCDNumber) && now->inherits("QLCDNumber") && settings->contains(now->objectName() + ".QLCDNumber")) 

        { 

            qobject_cast<QLCDNumber *>(now)->display(settings->value(now->objectName() + ".QLCDNumber").toInt()); 

continue; 

        } 

if((controlFlags & controlFlagsQProgressBar) && now->inherits("QProgressBar") && settings->contains(now->objectName() + ".QProgressBar")) 

        { 

            qobject_cast<QProgressBar *>(now)->setValue(settings->value(now->objectName() + ".QProgressBar").toInt()); 

continue; 

        } 

if((controlFlags & controlFlagsQRadioButton) && now->inherits("QRadioButton") && settings->contains(now->objectName() + ".QRadioButton")) 

        { 

            qobject_cast<QRadioButton *>(now)->setChecked(settings->value(now->objectName() + ".QRadioButton").toBool()); 

continue; 

        } 

if((controlFlags & controlFlagsQCheckBox) && now->inherits("QCheckBox") && settings->contains(now->objectName() + ".QCheckBox")) 

        { 

            qobject_cast<QCheckBox *>(now)->setChecked(settings->value(now->objectName() + ".QCheckBox").toBool()); 

continue; 

        } 

    } 

if(controlFlags & controlFlagsRecursion) 

    { 

        foreach(QObject *now, children) 

        { 

if(now->inherits("QWidget")) 

            { 

                read(settings, qobject_cast<QWidget *>(now), controlFlags); 

            } 

        } 

    } 

    settings->endGroup(); 

} 

读取

QSettings settings("/Users/Jason/Test.txt", QSettings::IniFormat); 

JasonQt_Settings_WidgetDataControl::read(&settings, m_ui->centralWidget, JasonQt_Settings_WidgetDataControl::controlFlagsAll); 

保存

QSettings settings("/Users/Jason/Test.txt", QSettings::IniFormat); 

JasonQt_Settings_WidgetDataControl::save(&settings, m_ui->centralWidget, JasonQt_Settings_WidgetDataControl::controlFlagsAll); 

效果:

作者:wsj

给我留言

留言无头像?