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


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

pararw.h:

  1. #ifndef PARARW_H   
  2. #define PARARW_H   
  3.   
  4. #include <QDialog>   
  5.   
  6. class QLabel;  
  7. class QPushButton;  
  8. class QLineEdit;  
  9. class QComboBox;  
  10. class QSpinBox;  
  11.   
  12. class ParaRW : public QDialog  
  13. {  
  14.     Q_OBJECT  
  15. public:  
  16.     ParaRW();  
  17.       
  18.     QLabel *label1;  
  19.     QLabel *label2;  
  20.     QLabel *label3;  
  21.     QLabel *label4;  
  22.     QLabel *label5;  
  23.     QLabel *timeLabel;  
  24.     QPushButton *saveButton;  
  25.     QPushButton *getButton;  
  26.     QComboBox *powerComboBox;  
  27.     QSpinBox *channelSpinBox;  
  28.     QLineEdit *frequencyEdit;  
  29.       
  30. public slots:  
  31.     void slotSave();  
  32.     void slotGet();    
  33.      
  34. };  
  35.   
  36. #endif  // PARARW_H  

pararw.cpp:

  1. #include "pararw.h"   
  2. #include <QtGui>   
  3.   
  4. ParaRW::ParaRW()  
  5. {  
  6.     QFont f("ZYSong18030",12);  
  7.     setFont(f);  
  8.       
  9.     setWindowTitle(tr("QDataStrem"));  
  10.       
  11.     label1 = new QLabel(tr("Channel:"));  
  12.     label2 = new QLabel(tr("Power:"));  
  13.     label3 = new QLabel(tr("Frequency:"));  
  14.     label4 = new QLabel(tr("MHz"));  
  15.     label5 = new QLabel(tr("Last save time:"));  
  16.     timeLabel = new QLabel;  
  17.       
  18.     saveButton = new QPushButton(tr("Save"));  
  19.     getButton = new QPushButton(tr("Get"));  
  20.       
  21.     channelSpinBox = new QSpinBox;  
  22.     channelSpinBox->setRange(0,10);  
  23.     powerComboBox = new QComboBox;  
  24.     powerComboBox->insertItem(0,tr("small"));  
  25.     powerComboBox->insertItem(1,tr("mid"));  
  26.     powerComboBox->insertItem(2,tr("big"));  
  27.     frequencyEdit = new QLineEdit;  
  28.       
  29.     QGridLayout *layout = new QGridLayout(this);  
  30.     layout->setMargin(20);  
  31.     layout->setSpacing(10);  
  32.     layout->addWidget(label1,0,0);  
  33.     layout->addWidget(channelSpinBox,0,1);  
  34.     layout->addWidget(label2,1,0);  
  35.     layout->addWidget(powerComboBox,1,1);  
  36.     layout->addWidget(label3,2,0);  
  37.     layout->addWidget(frequencyEdit,2,1);  
  38.     layout->addWidget(label4,2,2);  
  39.     layout->addWidget(label5,0,2);  
  40.     layout->addWidget(timeLabel,0,3);  
  41.     layout->addWidget(saveButton,1,3);  
  42.     layout->addWidget(getButton,2,3);  
  43.       
  44.     connect(saveButton,SIGNAL(clicked()),this,SLOT(slotSave()));  
  45.     connect(getButton,SIGNAL(clicked()),this,SLOT(slotGet()));  
  46. }  
  47.   
  48. void  
  49. ParaRW::slotSave()  
  50. {  
  51.     int channel = channelSpinBox->value();  
  52.     int power = powerComboBox->currentIndex();  
  53.     float frequency = frequencyEdit->text().toFloat();  
  54.     QDateTime *time = new QDateTime;  
  55.       
  56.     QFile file("parameters.dat");  
  57.     file.open(QIODevice::WriteOnly);  
  58.     QDataStream out(&file);  
  59.       
  60.     out.setVersion(QDataStream::Qt_4_0);  
  61.     out << (quint32)0xa1a2a3a4;  
  62.       
  63.     out << (qint32)channel << (qint32)power << frequency << time->currentDateTime();  
  64. }  
  65.   
  66. void  
  67. ParaRW::slotGet()  
  68. {  
  69.     QFile file("parameters.dat");  
  70.     file.open(QIODevice::ReadOnly);  
  71.     QDataStream in(&file);  
  72.       
  73.     in.setVersion(QDataStream::Qt_4_0);  
  74.     qint32 magic;  
  75.     in >> magic;  
  76.     if (magic != 0xa1a2a3a4)  
  77.     {  
  78.         QMessageBox::information(this,"exception",tr("invalide file format"));  
  79.         return;  
  80.     }  
  81.       
  82.     qint32 channel;  
  83.     qint32 power;  
  84.     float frequency;  
  85.     QDateTime time;  
  86.     in >> channel >> power >> frequency >> time;  
  87.       
  88.     channelSpinBox->setValue(channel);  
  89.     powerComboBox->setCurrentIndex(power);  
  90.     QString freq;  
  91.     frequencyEdit->setText(freq.setNum(frequency));  
  92.     QString lastTime = time.date().toString(Qt::ISODate) + " " + time.time().toString();  
  93.     timeLabel->setText(lastTime);  
  94. }  
  • 1
  • 2
  • 下一页

相关内容