当前位置:网站首页>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
边栏推荐
- Bug of QQ browser article comment: the commentator is wrong
- Solution: you can ping others, but others can't ping me
- Is it safe to open a securities account? Is there any danger
- Leetcode problem solving series -- continuous positive sequence with sum as s (sliding window)
- Write an open source, convenient and fast database document query and generation tool with WPF
- Lumiprobe 双功能交联剂丨Sulfo-Cyanine5 双-NHS 酯
- code
- [noip2015] jumping stone
- Regular expression
- [acnoi2022] color ball
猜你喜欢

540. Single element in ordered array / 1684 Count the number of consistent strings

每周推荐短视频:警惕“现象”与“问题”相互混淆

隐私沙盒终于要来了

Force buckle day33

Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester

Lumiprobe biomolecular quantification - qudye Protein Quantification Kit

2、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》启动并运行您的本地环境

解决方案:可以ping别人,但是别人不能ping我

Leetcode-141环形链表

Lumiprobe lumizol RNA extraction reagent solution
随机推荐
PTA year of birth
磁盘的基本知识和基本命令
Operation of cmake under win
Basic concepts of binary tree
Memo - about C # generating barcode
AI 训练速度突破摩尔定律;宋舒然团队获得RSS 2022最佳论文奖
[acnoi2022] color ball
C operator overloads the query table
Regular expression
证券开户安全么,有没有什么样的危险呢
Leetcode203 移除链表元素
Halcon图片标定,使得后续图片处理过后变成与模板图片一样
How to find the optimal learning rate
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Mujoco XML modeling
540. Single element in ordered array
Privacy sandbox is finally coming
R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息
Weekly recommended short videos: be alert to the confusion between "phenomena" and "problems"
Roll out! Enlightenment!