【QT】文件对话框
来源:互联网 发布:hbase是关系数据库吗 编辑:程序博客网 时间:2024/06/02 02:26
实现功能:
- 程序主窗口如下图:
2.点击pushbutton“open”,弹出如下文件对话框:
3.选择已存在的文件,如“12345.txt”,点击确定。此时主窗口的LineEdit控件显示选择的文件绝对路径,并在plainedit中显示文件内容:
4.将plainedit内容保存至新建文件中,选择保存路径并输入新建文件名。点击pushbutton“save”,弹出对话框如下:
代码实现:
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>namespace Ui {class MainWindow;}class MainWindow : public QMainWindow{ Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow();private: Ui::MainWindow *ui;private slots: void Onbtnopen(); void Onbtnsave();};#endif // MAINWINDOW_H
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QDebug>#include <QFileDialog>#include <QString>#include <GBK.h>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); connect(ui->m_btnopen,SIGNAL(clicked()),this,SLOT(Onbtnopen())); connect(ui->m_btnsave,SIGNAL(clicked()),this,SLOT(Onbtnsave()));}MainWindow::~MainWindow(){ delete ui;}void MainWindow::Onbtnopen(){ //选择文件 QString filepath=QFileDialog::getOpenFileName(this,tr("选择文件")); ui->m_lineEdit->setText(filepath); string gbk_filepath=GBK::FromUnicode(filepath); //打开文件 FILE*fp=fopen(gbk_filepath.c_str(),"rb"); //计算文件大小 fseek(fp,0,SEEK_END); int filesize=ftell(fp); fseek(fp,0,SEEK_SET); //读入缓冲区 char *buf=new char[filesize+1]; int n=fread(buf,1,filesize,fp); if(n>0) { buf[n]=0; ui->m_plainTextEdit->setPlainText(GBK::ToUnicode(buf)); //显示给plaintext控件 } delete[]buf; //delete掉new的指针 fclose(fp); //关闭文件}void MainWindow::Onbtnsave(){ QString filename = QFileDialog::getSaveFileName(this,tr("保存为")); //保存为 文件地址 QString text=ui->m_plainTextEdit->toPlainText(); if(filename.length()>0) { FILE*fp=fopen(GBK::FromUnicode(filename).c_str(),"wb"); //打开文件 fwrite(GBK::FromUnicode(text).c_str(),1,text.length(),fp); //写文件 fclose(fp); //关闭文件 }}
阅读全文
0 0
- Qt 打开文件对话框
- QT打开文件对话框
- QT之文件对话框
- qt增加文件对话框
- Qt打开文件对话框
- QT的文件对话框
- 【Qt】标准文件对话框
- QT之文件对话框
- 【QT】文件对话框
- QT 打开文件对话框
- QT设置文件对话框
- Qt基本对话框之文件对话框,颜色对话框,字体对话框
- QT重写filedialog文件对话框
- QT 打开文件对话框总结
- QT 打开文件对话框总结
- 九、Qt文件选择对话框
- QT 打开文件对话框总结
- QT 打开文件对话框总结
- 003_详解docker架构
- 12.数据库概念相关
- 回头看Android之service
- cmake生成vs解决方案时的项目文件夹
- 函数和数组的小知识点笔记
- 【QT】文件对话框
- nagiosgraph的配置调整
- Leetcode 17. Letter Combinations of a Phone Number
- 程序员必须弄懂结构体,结构体基础知识
- linux下安装mxnet和python接口配置
- obc学习笔记2
- 1071. Speech Patterns (25)
- 面试题25. 二叉树中和为某一值的路径
- 玩转 Python 3.5 的 await/async