# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))
class Settings(QDialog):
def __init__(self,parent=None):
super(Settings,self).__init__(parent)
self.setWindowTitle(self.tr("利用QDataStream对文件进行存取"))
self.label = QLabel(self.tr("通道:"))
self.channelSpinBox = QSpinBox()
self.channelSpinBox.setRange(0,20)
self.timeLabel = QLabel(self.tr("第一次运行"))
self.label_gong =QLabel(self.tr("功率:"))
self.powerComboBox = QComboBox()
self.powerComboBox.addItem(self.tr("大"),"大")
self.powerComboBox.addItem(self.tr("中"),"中")
self.powerComboBox.addItem(self.tr("小"),"小")
self.saveButton = QPushButton(self.tr("保存"))
self.label_Frequency = QLabel(self.tr("频率:"))
self.FrequencyEdit = QLineEdit()
self.getButton = QPushButton(self.tr("读取"))
layout = QGridLayout(self)
layout.addWidget(self.label,0,0)
layout.addWidget(self.channelSpinBox,0,1)
layout.addWidget(self.timeLabel,0,2)
layout.addWidget(self.label_gong,1,0)
layout.addWidget(self.powerComboBox,1,1)
layout.addWidget(self.saveButton,1,2)
layout.addWidget(self.label_Frequency,2,0)
layout.addWidget(self.FrequencyEdit,2,1)
layout.addWidget(self.getButton,2,2)
self.resize(250,150)
self.connect(self.saveButton,SIGNAL("clicked()"),self.slotSave)
self.connect(self.getButton,SIGNAL("clicked()"),self.slotGet)
def slotSave(self):
self.channel = self.channelSpinBox.value()
self.power = self.powerComboBox.currentIndex()
self.frequency = self.FrequencyEdit.text()
self.time = QDateTime()
file = QFile("parameters.dat")
file.open(QIODevice.WriteOnly)
out = QDataStream(file)
out.setVersion(QDataStream.Qt_4_0)
out.writeUInt32(0xa1a2a3a4)
out.writeUInt32(self.channel)
out.writeUInt32(self.power)
out.writeString(self.frequency)
out << self.time.currentDateTime()
def slotGet(self):
file = QFile("parameters.dat")
file.open(QIODevice.ReadOnly)
In = QDataStream(file)
In.setVersion(QDataStream.Qt_4_0)
magic = In.readUInt32()
if magic != 0xa1a2a3a4:
QMessageBox.information(self,"exception",self.tr("invalid file format"))
return
channel = In.readUInt32()
power = In.readUInt32()
frequency = In.readString()
time = QDateTime()
In >> time
self.channelSpinBox.setValue(channel)
self.powerComboBox.setCurrentIndex(power)
self.FrequencyEdit.setText(frequency)
lastTime = time.date().toString(Qt.ISODate) + " " + time.time().toString()
self.timeLabel.setText(lastTime)
app=QApplication(sys.argv)
dialog=Settings()
dialog.show()
app.exec_()