Qt4之借助QDataStream对文件进行存取
发布时间:2021-11-24 20:54:33 所属栏目:教程 来源:互联网
导读:QDataStream提拱了一个二进制的数据流,并且与程序运行的操作系统平台无关。利用QDataStream类可以方便地保存和读取各类数据。例如,在实现应用中常需要保存用户设置的参数,以便下次运行时烣复关闭时的参数设置,可需要与其他程序交互参数等。 pararw.h: #if
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); } (编辑:常州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |