当前位置:网站首页>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
边栏推荐
- D. Yet Another Minimization Problem
- Lumiprobe非荧光炔烃丨EU(5-乙炔基尿苷)
- Mysql database design
- 关于企业中台规划和 IT 架构微服务转型
- Single element of an ordered array
- Leetcode problem solving series -- continuous positive sequence with sum as s (sliding window)
- MySQL connection tools
- (6) VIM editor MV cat redirection standard input and output more pipe symbols head tail
- [source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
- Leetcode-160相交链表
猜你喜欢

What designs are needed in the architecture to build a general monitoring and alarm platform

NSI packaging script add file details

t10_ Adapting to Market Participantsand Conditions

Android development interview was badly hit in 3 years, and now the recruitment technical requirements are so high?

Mujoco's biped robot Darwin model

Bernoulli distribution (a discrete distribution)

Bug of QQ browser article comment: the commentator is wrong

隐私沙盒终于要来了

Privacy sandbox is finally coming

What impact will multinational encryption regulation bring to the market in 2022
随机推荐
Leetcode203 移除链表元素
Bernoulli distribution (a discrete distribution)
Introduction to easyclick database
Opencv map reading test -- error resolution
Localization through custom services in the shuttle application
About enterprise middle office planning and it architecture microservice transformation
Lumiprobe非荧光炔烃丨EU(5-乙炔基尿苷)
LeetCode 148. Sort linked list
Operation of cmake under win
How to change guns for 2D characters
证券开户安全么,有没有什么样的危险呢
Financial judgment questions
1380. Lucky number in matrix / 1672 Total assets of the richest customers
力扣每日一题-第32天-589.N×树的前序遍历
code
Go Technology Daily (2022-02-14) - go language slice interview real questions 8 consecutive questions
C# SelfHost WebAPI (2)
OpenAI|视频预训练 (VPT):基于观看未标记的在线视频的行动学习
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
磁盘的基本知识和基本命令