当前位置:网站首页>Qfile read / write file operation in QT
Qfile read / write file operation in QT
2022-07-01 18:48:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
1. First, record it QString,QByteArray,char * Conversion between
(1) QString -> QByteArray
QString buf = "123";
QByteArray a = buf.toUtf8(); // chinese
a = buf.toLocal8Bit(); // Local code (2) QByteArray -> char *
char *b = a.data();(3) char * -> QString[ Network programming often involves ]
char *p = "abc";
QString c = QString(p);2.QFile Read and write files
widget.cpp Source code is as follows :
#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){
// File object
QFile file(path);
// Open file
bool isOK = file.open(QIODevice::ReadOnly);
if(isOK == true){
#if 0
// Reading documents , The default only recognizes UTF-8
QByteArray array = file.readAll();// Check more help documents
// Show to edit area
ui->textEdit->setText(array);
#endif
QByteArray array;
while (file.atEnd() == false) {
// Read a line
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){
// Create a file object
QFile file;
// Associated file name
file.setFileName(path);
// Open file , Just write the way
bool isOK = file.open(QIODevice::WriteOnly);
if (isOK == true){
// Get the contents of the edit area
QString str = ui->textEdit->toPlainText();
//write files
//QString -> QByteArray
// file.write(str.toUtf8());
//QString -> C++ string ->char*
file.write(str.toStdString().data());
}
}
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/130796.html Link to the original text :https://javaforall.cn
边栏推荐
- 字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
- LeetCode-21合并两个有序链表
- Facebook聊单,SaleSmartly有妙招!
- js找出数字在数组中下一个相邻的元素
- Is Alipay wallet convenient to use?
- Sanfeng cloud 0215 I often use
- D. Yet Another Minimization Problem
- 搭建一個通用監控告警平臺,架構上需要有哪些設計
- NSI packaging script add file details
- The ultimate version of the 13th simulation of the single chip microcomputer provincial competition of the Blue Bridge Cup
猜你喜欢

Lumiprobe 生物分子定量丨QuDye 蛋白定量试剂盒

Sum of three numbers

Principal component calculation weight
![[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out](/img/f3/20b73f3545cdd17b9fbc52bf493ab4.jpg)
[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out

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

2. Create your own NFT collections and publish a Web3 application to show them start and run your local environment

3、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》在本地铸造 NFT

Go Technology Daily (2022-02-14) - go language slice interview real questions 8 consecutive questions

Lumiprobe 双功能交联剂丨Sulfo-Cyanine5 双-NHS 酯

宏观视角看抖音全生态
随机推荐
12. Design of power divider for ads usage record
Write it down once Net travel management background CPU Explosion Analysis
R语言使用dplyr包的transmute函数计算dataframe数据中的指定数据列的移动窗口均值、使用ggplot2包可视化移动均值与原始数据的折线图
Lumiprobe lumizol RNA extraction reagent solution
Create your own NFT collections and publish a Web3 application to show them (Introduction)
Livedata postvalue will "lose" data
毕业总结
R语言epiDisplay包ordinal.or.display函数获取有序logistic回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)、write.csv函数保存csv
主成分之综合竞争力案例分析
Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester
Lumiprobe非荧光炔烃丨EU(5-乙炔基尿苷)
如何运营好技术相关的自媒体?
What designs are needed in the architecture to build a general monitoring and alarm platform
R语言ggplot2可视化:gganimate包transition_time函数创建动态散点图动画(gif)、shadow_wake函数配置动画的渐变效果(gradual falloff)拖尾效应
Localization through custom services in the shuttle application
Lumiprobe biomolecular quantification - qudye Protein Quantification Kit
Thread forced join, thread forced join application scenarios
Memo - about C # generating barcode
How to manage 1000 anchors by one person?
OpenAI|视频预训练 (VPT):基于观看未标记的在线视频的行动学习