当前位置:网站首页>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
边栏推荐
- Weekly recommended short videos: be alert to the confusion between "phenomena" and "problems"
- Draw drawing process of UI drawing process
- 12. Design of power divider for ads usage record
- Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
- 2、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》启动并运行您的本地环境
- Popular science: what does it mean to enter the kernel state?
- 搭建一個通用監控告警平臺,架構上需要有哪些設計
- Database - MySQL advanced SQL statement (I)
- Go Technology Daily (2022-02-14) - go language slice interview real questions 8 consecutive questions
- LeetCode 148. Sort linked list
猜你喜欢
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
Leetcode-83 删除排序链表中重复的元素
Weekly recommended short videos: be alert to the confusion between "phenomena" and "problems"
MySQL connection tools
Five degrees easy chain enterprise app is newly upgraded
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
关于企业中台规划和 IT 架构微服务转型
Lumiprobe Lumizol RNA 提取试剂解决方案
Navicat premium 15 permanent cracking and 2021 latest idea cracking (valid for personal testing)
C# SelfHost WebAPI (2)
随机推荐
12种数据量纲化处理方式
Go Technology Daily (2022-02-14) - go language slice interview real questions 8 consecutive questions
Lumiprobe biomolecular quantification - qudye Protein Quantification Kit
How to find the optimal learning rate
Facebook聊单,SaleSmartly有妙招!
Reading notes series "modern methods of C language programming" -- Chapter 2
Mysql database of easyclick
Single element of an ordered array
Financial judgment questions
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
Localization through custom services in the shuttle application
MySQL connection tools
Unity learning fourth week
主成分计算权重
R language uses follow up of epidisplay package Plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses n.of The lines parameter specifies the num
Localization through custom services in the shuttle application
Vidéos courtes recommandées chaque semaine: méfiez - vous de la confusion entre « phénomène » et « problème »
Case study on comprehensive competitiveness of principal components
How to change guns for 2D characters
《Go题库·16》读写锁底层是怎么实现的