当前位置:网站首页>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
边栏推荐
- Rust language - cargo, crates io
- [Chongqing Guangdong education] basic psychology reference materials of Tianjin Normal University
- Mujoco model learning record
- Popular science: what does it mean to enter the kernel state?
- Definition of rotation axis in mujoco
- Mujoco's biped robot Darwin model
- Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
- [today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out
- R语言使用epiDisplay包的tableStack函数制作统计汇总表格(基于目标变量分组的描述性统计、假设检验等)、不设置by参数则计算数据框指定数据列范围的基础描述性统计信息
- Localization through custom services in the shuttle application
猜你喜欢
用GSConv+Slim Neck改进Yolov5,将性能提升到极致!
Lumiprobe non fluorescent alkyne EU (5-ethynyluridine)
1380. Lucky number in matrix / 1672 Total assets of the richest customers
Halcon image calibration enables subsequent image processing to become the same as the template image
Find all missing numbers in the array
信度系数低怎么办?信度系数具体怎么算?
Lumiprobe lumizol RNA extraction reagent solution
Mysql database of easyclick
每周推薦短視頻:警惕“現象”與“問題”相互混淆
磁盘的基本知识和基本命令
随机推荐
Lumiprobe non fluorescent alkyne EU (5-ethynyluridine)
Search 2D matrix 2
Case study on comprehensive competitiveness of principal components
How to change guns for 2D characters
3、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》在本地铸造 NFT
Go Technology Daily (2022-02-14) - go language slice interview real questions 8 consecutive questions
因子分析怎么计算权重?
1380. Lucky number in matrix / 1672 Total assets of the richest customers
12. Design of power divider for ads usage record
Blue Bridge Cup real topic: the shortest circuit
Write an open source, convenient and fast database document query and generation tool with WPF
The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
Leetcode problem solving series -- continuous positive sequence with sum as s (sliding window)
t10_ Adapting to Market Participantsand Conditions
Single element of an ordered array
Mise en place d'une plate - forme générale de surveillance et d'alarme, quelles sont les conceptions nécessaires dans l'architecture?
Vidéos courtes recommandées chaque semaine: méfiez - vous de la confusion entre « phénomène » et « problème »
Navicat premium 15 permanent cracking and 2021 latest idea cracking (valid for personal testing)
Flex layout
Localization through custom services in the shuttle application