【PyQt4 实例29】利用QDataStream对文件进行存取

本文介绍了一个使用PyQt4中的QDataStream类实现文件数据存取的简单应用程序。该程序允许用户设置通道号、选择功率等级、输入频率,并将这些数据连同当前时间保存到文件中。此外,还提供了从文件中读取并显示先前保存的数据的功能。
# -*- 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_()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值