当前位置:网站首页>Qt中的QFile读写文件操作
Qt中的QFile读写文件操作
2022-07-01 18:35:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
1.首先记录一下QString,QByteArray,char * 之间的转换
(1) QString -> QByteArray
QString buf = "123";
QByteArray a = buf.toUtf8(); //中文
a = buf.toLocal8Bit(); //本地编码(2) QByteArray -> char *
char *b = a.data();(3) char * -> QString[网络编程常常涉及到]
char *p = "abc";
QString c = QString(p);2.QFile读写文件
widget.cpp源码如下:
#include "widget.h"
#include "ui_widget.h"
#include<QFile>
#include<QFileDialog>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_buttonRead_clicked()
{
QString path = QFileDialog::getOpenFileName(this,
"open",
"../",
"TXT(*.txt)");
if(path.isEmpty() == false){
//文件对象
QFile file(path);
//打开文件
bool isOK = file.open(QIODevice::ReadOnly);
if(isOK == true){
#if 0
//读文件,默认只识别UTF-8
QByteArray array = file.readAll();//多查看帮助文档
//显示到编辑区
ui->textEdit->setText(array);
#endif
QByteArray array;
while (file.atEnd() == false) {
//读一行
array += file.readLine();
ui->textEdit->setText(array);
}
}
file.close();
}
}
void Widget::on_buttonSave_clicked()
{
QString path = QFileDialog::getSaveFileName(this, "save",
"../", "TXT(*.txt)");
if (path.isEmpty() == false){
//创建文件对象
QFile file;
//关联文件名字
file.setFileName(path);
//打开文件,只写方式
bool isOK = file.open(QIODevice::WriteOnly);
if (isOK == true){
//获取编辑区内容
QString str = ui->textEdit->toPlainText();
//write files
//QString -> QByteArray
// file.write(str.toUtf8());
//QString -> C++ string ->char*
file.write(str.toStdString().data());
}
}
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/130796.html原文链接:https://javaforall.cn
边栏推荐
- C language learning notes: type definition typedef and declaration external CSDN creation punch in
- Privacy sandbox is finally coming
- [source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
- Find all missing numbers in the array
- Rust language - cargo, crates io
- 解决方案:可以ping别人,但是别人不能ping我
- code
- [source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
- (6) VIM editor MV cat redirection standard input and output more pipe symbols head tail
- Definition of rotation axis in mujoco
猜你喜欢

Sum of three numbers

How to find customers for investment attraction in industrial parks

Solution: you can ping others, but others can't ping me

Vidéos courtes recommandées chaque semaine: méfiez - vous de la confusion entre « phénomène » et « problème »

Privacy sandbox is finally coming

Data query language (DQL)

Three.js学习-相机Camera的基本操作(了解向)

ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享

C# SelfHost WebAPI (2)

What impact will multinational encryption regulation bring to the market in 2022
随机推荐
Using OpenSSL encryption to rebound shell traffic
搭建一個通用監控告警平臺,架構上需要有哪些設計
On the language internationalization of Yongzhong Office
12. Design of power divider for ads usage record
Thread forced join, thread forced join application scenarios
Force buckle day33
证券开户安全么,有没有什么样的危险呢
Weekly recommended short videos: be alert to the confusion between "phenomena" and "problems"
R语言epiDisplay包ordinal.or.display函数获取有序logistic回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)、write.csv函数保存csv
Regular expression
Android development interview was badly hit in 3 years, and now the recruitment technical requirements are so high?
540. Single element in ordered array
信度系数低怎么办?信度系数具体怎么算?
The ultimate version of the 13th simulation of the single chip microcomputer provincial competition of the Blue Bridge Cup
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用n.of.lines参数指定显示的病例数
R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息
How does factor analysis calculate weights?
Mujoco's biped robot Darwin model
GAMES202作业0-环境搭建过程&解决遇到的问题
Go Technology Daily (2022-02-14) - go language slice interview real questions 8 consecutive questions