Qt4之利用QDataStream对文件进行存取

QDataStream提拱了一个二进制的数据流,并且与程序运行的操作系统平台无关。利用QDataStream类可以方便地保存和读取各类数据。例如,在实现应用中常需要保存用户设置的参数,以便下次运行时烣复关闭时的参数设置,可需要与其他程序交互参数等。


pararw.h:

#ifndef PARARW_H
#define PARARW_H

#include <QDialog>

class QLabel;
class QPushButton;
class QLineEdit;
class QComboBox;
class QSpinBox;

class ParaRW : public QDialog
{
    Q_OBJECT
public:
    ParaRW();
    
    QLabel *label1;
    QLabel *label2;
    QLabel *label3;
    QLabel *label4;
    QLabel *label5;
    QLabel *timeLabel;
    QPushButton *saveButton;
    QPushButton *getButton;
    QComboBox *powerComboBox;
    QSpinBox *channelSpinBox;
    QLineEdit *frequencyEdit;
    
public slots:
    void slotSave();
    void slotGet();  
   
};

#endif  // PARARW_H


pararw.cpp:

#include "pararw.h"
#include <QtGui>

ParaRW::ParaRW()
{
    QFont f("ZYSong18030",12);
    setFont(f);
    
    setWindowTitle(tr("QDataStrem"));
    
    label1 = new QLabel(tr("Channel:"));
    label2 = new QLabel(tr("Power:"));
    label3 = new QLabel(tr("Frequency:"));
    label4 = new QLabel(tr("MHz"));
    label5 = new QLabel(tr("Last save time:"));
    timeLabel = new QLabel;
    
    saveButton = new QPushButton(tr("Save"));
    getButton = new QPushButton(tr("Get"));
    
    channelSpinBox = new QSpinBox;
    channelSpinBox->setRange(0,10);
    powerComboBox = new QComboBox;
    powerComboBox->insertItem(0,tr("small"));
    powerComboBox->insertItem(1,tr("mid"));
    powerComboBox->insertItem(2,tr("big"));
    frequencyEdit = new QLineEdit;
    
    QGridLayout *layout = new QGridLayout(this);
    layout->setMargin(20);
    layout->setSpacing(10);
    layout->addWidget(label1,0,0);
    layout->addWidget(channelSpinBox,0,1);
    layout->addWidget(label2,1,0);
    layout->addWidget(powerComboBox,1,1);
    layout->addWidget(label3,2,0);
    layout->addWidget(frequencyEdit,2,1);
    layout->addWidget(label4,2,2);
    layout->addWidget(label5,0,2);
    layout->addWidget(timeLabel,0,3);
    layout->addWidget(saveButton,1,3);
    layout->addWidget(getButton,2,3);
    
    connect(saveButton,SIGNAL(clicked()),this,SLOT(slotSave()));
    connect(getButton,SIGNAL(clicked()),this,SLOT(slotGet()));
}

void
ParaRW::slotSave()
{
    int channel = channelSpinBox->value();
    int power = powerComboBox->currentIndex();
    float frequency = frequencyEdit->text().toFloat();
    QDateTime *time = new QDateTime;
    
    QFile file("parameters.dat");
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);
    
    out.setVersion(QDataStream::Qt_4_0);
    out << (quint32)0xa1a2a3a4;
    
    out << (qint32)channel << (qint32)power << frequency << time->currentDateTime();
}

void
ParaRW::slotGet()
{
    QFile file("parameters.dat");
    file.open(QIODevice::ReadOnly);
    QDataStream in(&file);
    
    in.setVersion(QDataStream::Qt_4_0);
    qint32 magic;
    in >> magic;
    if (magic != 0xa1a2a3a4)
    {
    	QMessageBox::information(this,"exception",tr("invalide file format"));
    	return;
    }
    
    qint32 channel;
    qint32 power;
    float frequency;
    QDateTime time;
    in >> channel >> power >> frequency >> time;
    
    channelSpinBox->setValue(channel);
    powerComboBox->setCurrentIndex(power);
    QString freq;
    frequencyEdit->setText(freq.setNum(frequency));
    QString lastTime = time.date().toString(Qt::ISODate) + " " + time.time().toString();
    timeLabel->setText(lastTime);
}

main.cpp:

#include <QApplication>
#include <QTranslator>

#include "pararw.h"

int main(int argc, char *argv[])
{
 //   QFont f("ZYSong18030",12);
 //   QApplication::setFont(f);
    QApplication app(argc,argv);
    
    QTranslator translator;
    translator.load("datastream_zh");
    app.installTranslator(&translator);
    
    ParaRW *para = new ParaRW();
    para->show();
    return app.exec();
}


datastream.pro:

######################################################################
# Automatically generated by qmake (2.01a) ?? ?? 10 19:03:31 2008
######################################################################

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += pararw.h
SOURCES += main.cpp pararw.cpp
TRANSLATIONS += datastream_zh.ts


实现效果跟QSetting相似。效果略。

你可能感兴趣的:(exception,File,layout,qt,float,Signal)