1 #-*-coding:utf-8-*-2 fromPyqt4.qtguiImport*3 fromPyqt4.qtcoreImport*4 ImportSYS5QTEXTCODEC.SETCODECFORTR (Qtextcodec.codecforname ("UTF8"))6 7 classSettings (qdialog):8 def __init__(self,parent=None):9Super (Settings,self).__init__(parent)TenSelf.setwindowtitle (self.tr ("access to files using Qdatastream")) One ASelf.label = Qlabel (self.tr ("Channel:")) -Self.channelspinbox =Qspinbox () -Self.channelSpinBox.setRange (0,20) theSelf.timelabel = Qlabel (self.tr ("run for the first time")) - -Self.label_gong =qlabel (self.tr ("Power:")) -Self.powercombobox =Qcombobox () +Self.powerComboBox.addItem (self.tr ("Big"),"Big") -Self.powerComboBox.addItem (self.tr ("in"),"in") +Self.powerComboBox.addItem (self.tr ("Small"),"Small") ASelf.savebutton = Qpushbutton (self.tr ("Save")) at -Self.label_frequency = Qlabel (self.tr ("Frequency:")) -Self. Frequencyedit =Qlineedit () -Self.getbutton = Qpushbutton (self.tr ("Read")) - -Layout =qgridlayout (self) in layout.addwidget (self.label,0,0) -Layout.addwidget (self.channelspinbox,0,1) toLayout.addwidget (self.timelabel,0,2) + -Layout.addwidget (self.label_gong,1, 0) theLayout.addwidget (self.powercombobox,1,1) *Layout.addwidget (self.savebutton,1,2) $ Panax NotoginsengLayout.addwidget (self.label_frequency,2, 0) -Layout.addwidget (self. frequencyedit,2,1) theLayout.addwidget (self.getbutton,2,2) + ASelf.resize (250,150) the +Self.connect (Self.savebutton,signal ("clicked ()"), Self.slotsave) -Self.connect (Self.getbutton,signal ("clicked ()"), Self.slotget) $ $ defSlotsave (self): -Self.channel =Self.channelSpinBox.value () -Self.power =Self.powerComboBox.currentIndex () theSelf.frequency =Self . Frequencyedit.text () -Self.time =qdatetime ()WuyiFile = QFile ("Parameters.dat") the File.Open (qiodevice.writeonly) -out =qdatastream (file) Wu out.setversion (QDATASTREAM.QT_4_0) - AboutOut.writeuint32 (0XA1A2A3A4) $ Out.writeuint32 (Self.channel) - Out.writeuint32 (self.power) - out.writestring (self.frequency) -Out <<self.time.currentDateTime () A + defSlotget (self): theFile = QFile ("Parameters.dat") - File.Open (qiodevice.readonly) $in =qdatastream (file) the in.setversion (QDATASTREAM.QT_4_0) theMagic =In.readuint32 () the ifMagic! = 0XA1A2A3A4: theQmessagebox.information (Self,"Exception", self.tr ("Invalid file format")) - return inChannel =In.readuint32 () thePower =In.readuint32 () thefrequency =in.readstring () AboutTime =qdatetime () theIn >> Time the Self.channelSpinBox.setValue (channel) the Self.powerComboBox.setCurrentIndex (Power) + Self . Frequencyedit.settext (Frequency) -Lasttime = Time.date (). toString (qt.isodate) +" "+time.time (). toString () the Self.timeLabel.setText (lasttime)Bayi theCp2=qapplication (SYS.ARGV) thedialog=Settings () - dialog.show () -APP.EXEC_ ()
"Reprint" PYQT use Qdatastream to access files