歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Qt4之利用QDataStream對文件進行存取

Qt4之利用QDataStream對文件進行存取

日期:2017/3/1 11:09:16   编辑:Linux編程

QDataStream提拱了一個二進制的數據流,並且與程序運行的操作系統平台無關。利用QDataStream類可以方便地保存和讀取各類數據。例如,在實現應用中常需要保存用戶設置的參數,以便下次運行時烣復關閉時的參數設置,可需要與其他程序交互參數等。

pararw.h:

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

pararw.cpp:

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