QT实现对文件的操作

  • 格式:doc
  • 大小:201.50 KB
  • 文档页数:11

中北大学Linux Qt设计说明书学生姓名: QQ 学号:学院:专业:题目: QT实现对文件的操作成绩指导教师秦品乐2012年5月16日1.设计目的在linux平台,通过Qt Designer软件,基于c++语言,开发学生管理系统。

2.设计内容和要求功能1,实现文件的插入操作。

功能2,实现文件的删除操作。

功能3,实现文件的修改操作。

功能4,实现文件的查找操作。

3.结果和详细设计代码/**************************************************************************** ** Form implementation generated from reading ui file 'form1.ui'**** Created: 六 5月 12 09:57:37 2012** by: The User Interface Compiler ($Id: qt/main.cpp 3.1.1 edited Nov 21 17:40 $)**** WARNING! All changes made in this file will be lost!****************************************************************************/Form1::Form1( QWidget* parent, const char* name, WFlags fl ): QMainWindow( parent, name, fl ){(void)statusBar();if ( !name )setName( "Form1" );fileNewAction = new QAction( this, "fileNewAction" );fileOpenAction = new QAction( this, "fileOpenAction" );fileSaveAction = new QAction( this, "fileSaveAction" );fileSaveAsAction = new QAction( this, "fileSaveAsAction" );form2 = new Form2(this);form3 = new Form3(this);form4 = new Form4(this);form5 = new Form5(this);menubar = new QMenuBar( this, "menubar" );fileMenu = new QPopupMenu( this );fileNewAction->addTo( fileMenu );fileOpenAction->addTo( fileMenu );fileSaveAction->addTo( fileMenu );fileSaveAsAction->addTo( fileMenu );fileMenu->insertSeparator();fileMenu->insertSeparator();menubar->insertItem( "", fileMenu, 0 );languageChange();resize( QSize(600, 480).expandedTo(minimumSizeHint()) );connect( fileNewAction, SIGNAL( activated() ), this, SLOT( fileNew() ) ); connect( fileOpenAction, SIGNAL( activated() ), this, SLOT( fileOpen() ) ); connect( fileSaveAction, SIGNAL( activated() ), this, SLOT( fileSave() ) ); connect( fileSaveAsAction, SIGNAL( activated() ), this, SLOT( fileSaveAs() ) ); }Form1::~Form1(){}void Form1::fileNew(){ form2->show();}void Form1::fileOpen(){form3->show();}void Form1::fileSave(){form4->show();}void Form1::fileSaveAs(){form5->show();}Form2::Form2( QWidget* parent, const char* name, WFlags fl ): QMainWindow( parent, name, fl ){(void)statusBar();if ( !name )setName( "Form2" );setCentralWidget( new QWidget( this, "qt_central_widget" ) );textEdit4 = new QTextEdit( centralWidget(), "textEdit4" );textEdit4->setGeometry( QRect( 80, 59, 201, 241 ) );lineEdit5 = new QLineEdit( centralWidget(), "lineEdit5" );lineEdit5->setGeometry( QRect( 320, 99, 201, 61 ) );pushButton4 = new QPushButton( centralWidget(), "pushButton4" ); pushButton4->setGeometry( QRect( 360, 209, 111, 41 ) );languageChange();resize( QSize(600, 480).expandedTo(minimumSizeHint()) );connect( pushButton4, SIGNAL( clicked() ), this, SLOT( Add() ) ); fileshow();}Form2::~Form2(){}void Form2::languageChange(){setCaption( tr( "Add" ) );pushButton4->setText( tr( "Add" ) ); }void Form2::Add(){QFile f1("a.txt");if(f1.open(IO_WriteOnly|IO_Append)) {QTextStream stream(&f1);stream << lineEdit5->text();stream << "\n";}f1.close();fileshow();}void Form2::fileshow(){QString string;QFile f("a.txt");if(f.open(IO_ReadOnly)){QTextStream s(&f);while(!s.atEnd()){string+=s.readLine();string+="\n";}textEdit4->setText(string);}f.close();}Form3::Form3( QWidget* parent, const char* name, WFlags fl ) : QMainWindow( parent, name, fl ){(void)statusBar();if ( !name )setName( "Form3" );setCentralWidget( new QWidget( this, "qt_central_widget" ) );textEdit3 = new QTextEdit( centralWidget(), "textEdit3" );textEdit3->setGeometry( QRect( 80, 69, 201, 241 ) );lineEdit3 = new QLineEdit( centralWidget(), "lineEdit3" );lineEdit3->setGeometry( QRect( 320, 99, 211, 51 ) );pushButton3 = new QPushButton( centralWidget(), "pushButton3" );pushButton3->setGeometry( QRect( 370, 199, 101, 41 ) );languageChange();resize( QSize(600, 480).expandedTo(minimumSizeHint()) );connect( pushButton3, SIGNAL( clicked() ), this, SLOT( del() ) );fileshow();}Form3::~Form3(){}void Form3::languageChange(){setCaption( tr( "Del" ) );pushButton3->setText( tr( "Del" ) ); }void Form3::del(){QString line;QString lines;QFile file("a.txt");file.open(IO_ReadOnly);QTextStream stream(&file);QString s=lineEdit3->text();while(!stream.atEnd()){ line=stream.readLine();if(line.contains(s)==1){line=" ";}else{lines+=line;lines+="\n";}}textEdit3->setText(lines);file.close();updata();}void Form3::updata(){QFile file("a.txt");if (file.open(IO_WriteOnly)){QTextStream stream(&file);stream<<textEdit3->text();stream<<"\n";}file.close();}void Form3::fileshow(){QString string;QFile f("a.txt");if(f.open(IO_ReadOnly)){QTextStream s(&f);while(!s.atEnd()){string+=s.readLine();string+="\n";}textEdit3->setText(string);}}Form4::Form4( QWidget* parent, const char* name, WFlags fl ) : QMainWindow( parent, name, fl ){(void)statusBar();if ( !name )setName( "Form4" );setCentralWidget( new QWidget( this, "qt_central_widget" ) );textEdit2 = new QTextEdit( centralWidget(), "textEdit2" );textEdit2->setGeometry( QRect( 80, 69, 201, 241 ) );lineEdit2 = new QLineEdit( centralWidget(), "lineEdit2" );lineEdit2->setGeometry( QRect( 330, 129, 181, 51 ) );pushButton2 = new QPushButton( centralWidget(), "pushButton2" );pushButton2->setGeometry( QRect( 370, 219, 101, 41 ) );languageChange();resize( QSize(600, 480).expandedTo(minimumSizeHint()) );connect( pushButton2, SIGNAL( clicked() ), this, SLOT( modefy() ) );fileshow();}Form4::~Form4(){}void Form4::languageChange(){setCaption( tr( "Modefy" ) );pushButton2->setText( tr( "Modefy" ) );}void Form4::modefy(){QFile file("a.txt");if (file.open(IO_WriteOnly)){QTextStream stream(&file);stream<<textEdit2->text();stream<<"\n";}file.close();fileshow();}void Form4::fileshow(){QString string;QFile f("a.txt");if(f.open(IO_ReadOnly)){QTextStream s(&f);while(!s.atEnd()){string+=s.readLine();string+="\n";}textEdit2->setText(string);}f.close();}Form5::Form5( QWidget* parent, const char* name, WFlags fl ) : QMainWindow( parent, name, fl ){(void)statusBar();if ( !name )setName( "Form5" );setCentralWidget( new QWidget( this, "qt_central_widget" ) );textEdit1 = new QTextEdit( centralWidget(), "textEdit1" );textEdit1->setGeometry( QRect( 50, 59, 231, 251 ) );lineEdit1 = new QLineEdit( centralWidget(), "lineEdit1" );lineEdit1->setGeometry( QRect( 340, 89, 141, 51 ) );pushButton1 = new QPushButton( centralWidget(), "pushButton1" );pushButton1->setGeometry( QRect( 360, 199, 91, 41 ) );languageChange();resize( QSize(600, 480).expandedTo(minimumSizeHint()) ); connect(pushButton1,SIGNAL(clicked()),this,SLOT(search()));}Form5::~Form5(){}void Form5::languageChange(){setCaption( tr( "Search" ) );pushButton1->setText( tr( "Search" ) );}void Form5::search(){QString line;QFile file("a.txt");file.open(IO_ReadOnly);QTextStream stream(&file);QString s=lineEdit1->text();while(!stream.atEnd()){ line=stream.readLine();{ if(line.contains(s)==1)textEdit1->setText(line);}}file.close();}5.课程设计心得及存在问题通过这次系统的设计,我初步了解了linux系统,用Qt Designer开发软件的过程。