QT |
您所在的位置:网站首页 › 替换功能无法实现的操作 › QT |
用qt的QFile类实现一系列对文件的简单操作
1.打开文件代码实现效果展示
2.写入文件代码实现效果展示
3.文件修改代码实现效果展示
4.删除文件代码实现效果展示
此系列展示均为创建有ui界面的工程,但不对ui界面进行操作,且实例基类均mainwindow
1.打开文件
代码实现
mainwindow.h中添加一个行编辑框和按钮控件作为简单界面布局,并添加简单的显示关联槽函数。 #include #include private: QLineEdit *filename; QPushButton *button; private slots: void showFiles();maindwindow.cpp中对其实现 添加头文件 #include //实例单行文本控件 filename = new QLineEdit(this); //位置 filename->setGeometry(QRect(50,50,230,25)); //实例按钮 button = new QPushButton(this); //按钮位置 button->setGeometry(QRect(280,50,80,25)); //按钮名 button->setText("浏览"); //按钮点击事件 connect(button,SIGNAL(clicked()),this,SLOT(showFiles()));槽函数实现 //按钮点击方法 void MainWindow::showFiles() { //定义变量str 接收QFileDialog 对话框获取的文件路径 QString str = QFileDialog::getOpenFileName(this,"文件游览","/","textfile(*.txt);;C file(*.cpp);;All file(*.*)"); //将变量绑定QLineEdit 控件 filename->setText(str.toUtf8()); } 效果展示需要注意的是,此工程文件创建在E盘中,所以文件定位到E盘的根目录下,这里作为例子不对其做深究, 2.写入文件 代码实现mainwindow.h中同样添加行编辑框和按钮控件作为界面主要内容 #include #include private: QPushButton *button; QLineEdit *edit; QLineEdit *content; private slots: void createFile();mainwindow.cpp 添加头文件 #include #include //文件操作————写入文件内容 //实例 edit = new QLineEdit(this); //位置 edit->setGeometry(QRect(50,50,200,25)); //值,可改变相应的位置 edit->setText("D:/Qt02.txt"); //实例 content = new QLineEdit(this); //位置 content->setGeometry(QRect(50,100,200,25)); //值 content->setText("写入文件的内容"); //实例button button = new QPushButton(this); //位置 button->setGeometry(QRect(270,50,80,25)); //值 button->setText("创建"); //事件 connect(button,SIGNAL(clicked()),this,SLOT(createFile()));槽函数实现 void MainWindow::createFile() { //实例QFile QFile file(edit->text()); //判断文件是否存在 if(file.exists()) { QMessageBox::warning(this,"创建文件","文件已经存在!"); }else { //存在打开,不存在创建 file. open (QIODevice::ReadWrite | QIODevice::Text); //写入内容,这里需要转码,否则报错。 QByteArray str = content->text().toUtf8(); //写入QByteArray 格式字符串 file.write(str); //提示成功 QMessageBox::warning(this,"创建文件","文件创建成功!"); } //关闭文件 file.close (); } 效果展示在D盘中故可以找到新建的文件 3.文件修改 代码实现mainwindow.h 添加头文件 #include #include private: QPushButton *browseBt; QPushButton *saveBt; QTextEdit *edit; QTextEdit *content; private slots: void browseFile(); //游览文件 void saveFile(); //保存文件mainwindow.cpp 添加头文件 #include #include //实例文件路径 edit = new QTextEdit(this); edit->setGeometry(QRect(50,50,240,26)); //浏览文件按钮 browseBt = new QPushButton(this); browseBt->setGeometry(QRect(290,50,80,25)); browseBt->setText("浏览文件"); connect(browseBt,SIGNAL(clicked()),this,SLOT(browseFile())); //显示文件内容 content = new QTextEdit(this); content->setGeometry(QRect(50,80,320,150)); //修改完毕后,保存文件 saveBt = new QPushButton(this); saveBt->setGeometry(QRect(290,240,80,25)); saveBt->setText("保存"); connect(saveBt,SIGNAL(clicked()),this,SLOT(saveFile()));槽函数实现 //浏览文件 void MainWindow::browseFile() { //QTextCodec * codec = QTextCodec::codecForName("GBk"); //定义变量str 接收QFileDialog 对话框获取的文件路径 QString str = QFileDialog::getOpenFileName(this,"open file","/","textfile(*.txt);;C file(*.cpp);;All file(*.*)"); //将变量绑定QTextEdit 控件 edit->setText(str.toUtf8()); //判断是否选择文件 if(edit->toPlainText().isEmpty()) { return; } QFile file(edit->toPlainText()); //判断文件是否打开成功 if(!file. open (QIODevice::ReadOnly|QIODevice::Text)) { QMessageBox::warning(this,"打开文件","打开文件失败!"); return; } QTextStream ts(&file); //文件转码为utf-8的形式 ts.setCodec("UTF-8"); //循环文档数据至结尾 while(!ts.atEnd()) { //将全部数据绑定至content 控件 content->setPlainText(ts.readAll()); } //关闭文档 file. close (); } //保存文件 void MainWindow::saveFile() { QFile file(edit->toPlainText()); file. open (QIODevice::ReadWrite | QIODevice::Text); //写入内容,这里需要转码,否则报错。 QByteArray str = content->toPlainText().toUtf8(); //写入QByteArray 格式字符串 file.write(str); //提示成功 QMessageBox::warning(this,"修改文件","文件修改成功!"); file. close (); } 效果展示以下面的两个txt文档为例打开 需要注意的是,在这个程序中,由于所有qt支持编码为UTF-8。所以编写出的界面程序依旧是UTF-8形式的。而windows中所创建的txt文档是GBk编码的,所以在打开文件时候需要将GBK编码转换成程序支持的UTF-8编码。这样才不会出现乱码 //文件转码为utf-8的形式 ts.setCodec("UTF-8"); 4.删除文件 代码实现mainwindow.h 添加头文件 #include #include private: QPushButton *browseBt; QPushButton *deleteBt; QLineEdit *filePath; private slots: void browseFile(); void deleteFile();mainqwindow.cpp 添加头文件 #include filePath = new QLineEdit(this); filePath->setGeometry(QRect(50,50,200,25)); //游览文件按钮 browseBt = new QPushButton(this); browseBt->setGeometry(QRect(270,50,80,25)); browseBt->setText("游览文件"); connect(browseBt,SIGNAL(clicked()),this,SLOT(browseFile())); //删除文件按钮 deleteBt = new QPushButton(this); deleteBt->setGeometry(QRect(50,100,80,25)); deleteBt->setText("删除文件"); connect(deleteBt,SIGNAL(clicked()),this,SLOT(deleteFile())); **槽函数** void MainWindow::browseFile() { //定义变量str 接收QFileDialog 对话框获取的文件路径 QString str = QFileDialog::getOpenFileName(this,"open file","/home", "text file(*.txt);;C file(*.cpp);;All file(*.*)"); //将变量绑定QLineEdit控件 filePath->setText(str.toUtf8()); } void MainWindow::deleteFile() { //删除参数中的内容 QFile::remove(filePath->text()); } 效果展示这里删除刚才创建的text01.txt文件 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |