当前位置:网站首页>QT 打开文件 使用 QFileDialog 获取文件名称、内容等
QT 打开文件 使用 QFileDialog 获取文件名称、内容等
2022-07-06 20:28:00 【程序媛zcc】
主要代码:
//.h
#include <QFile>
#include <QFileDialog>
QFile *localFile;
QString filename;
qint64 totalBytes; //文件总字节数
QByteArray authorizeFile;
//.cpp
//先给按钮绑定打开文件
connect(this->ui->pushButton_openFile, SIGNAL(clicked()), this, SLOT(selectFile()));
void MyDialog::selectFile()
{
this->filename = QFileDialog::getOpenFileName(this, "Open a file", "/", "files (*)");
// ui->textEdit->setText(filename);
this->localFile = new QFile(filename);
if (!localFile->open(QFile::ReadOnly | QIODevice::Text))
{
ui->textEdit->setText(tr("FileSetDialog:open file error!"));
return;
}
// 获取文件大小
this->totalBytes = localFile->size();
qDebug() << "this->totalBytes====" << this->totalBytes;
//获取文件里所有内容
authorizeFile = localFile->readAll();
fileTxt = QString(authorizeFile);
ui->textEdit->setText(fileTxt);
}
1、点击打开文件按钮 弹出选择文件弹框
2、弹框按取消 提示
3、选择文件确定 把文件内容展示出来
边栏推荐
- VHDL implementation of arbitrary size matrix addition operation
- [untitled]
- [dream database] add the task of automatically collecting statistical information
- Open3D 网格滤波
- 【C语言】 题集 of Ⅸ
- 2022.6.28
- QT thread and other 01 concepts
- Delete data in SQL
- 20. (ArcGIS API for JS) ArcGIS API for JS surface collection (sketchviewmodel)
- Significance and measures of source code confidentiality
猜你喜欢
Decoration design enterprise website management system source code (including mobile source code)
About Confidence Intervals
When you go to the toilet, you can clearly explain the three Scheduling Strategies of scheduled tasks
概率论公式
[cpk-ra6m4 development board environment construction based on RT thread studio]
什么是 BA ?BA怎么样?BA和BI是什么关系?
Restcloud ETL Community Edition June featured Q & A
Depth analysis of compilation constants, classloader classes, and system class loaders
代码质量管理
体会设计细节
随机推荐
SSL证书错误怎么办?浏览器常见SSL证书报错解决办法
Huawei and Xiaomi "copy each other"
My brave way to line -- elaborate on what happens when the browser enters the URL
“去虚向实”大潮下,百度智能云向实而生
19. (ArcGIS API for JS) ArcGIS API for JS line acquisition (sketchviewmodel)
Flutter3.0了,小程序不止于移动应用跨端运行
Sorting operation partition, argpartition, sort, argsort in numpy
Ubuntu20 installation redisjson record
Tencent cloud native database tdsql-c was selected into the cloud native product catalog of the Academy of communications and communications
[dream database] add the task of automatically collecting statistical information
我的勇敢对线之路--详细阐述,浏览器输入URL发生了什么
VHDL实现任意大小矩阵加法运算
An error in SQL tuning advisor ora-00600: internal error code, arguments: [kesqsmakebindvalue:obj]
cocos3——8. Implementation Guide for beginners
25. (ArcGIS API for JS) ArcGIS API for JS line modification line editing (sketchviewmodel)
Flink Task退出流程与Failover机制
Opencv environment, and open a local PC camera.
Flink task exit process and failover mechanism
unrecognized selector sent to instance 0x10b34e810
Shell programming basics