当前位置:网站首页>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
边栏推荐
- Navicat Premium 15 永久破解和2021版本最新IDEA破解(亲测有效)
- Leetcode-83 delete duplicate elements in the sorting linked list
- R语言epiDisplay包ordinal.or.display函数获取有序logistic回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)、write.csv函数保存csv
- 信度系数低怎么办?信度系数具体怎么算?
- The R language uses the tablestack function of epidisplay package to make statistical summary tables (descriptive statistics based on the grouping of target variables, hypothesis testing, etc.). If th
- Reading notes series "modern methods of C language programming" -- Chapter 2
- R语言使用epiDisplay包的tableStack函数制作统计汇总表格(基于目标变量分组的描述性统计、假设检验等)、不设置by参数则计算数据框指定数据列范围的基础描述性统计信息
- R language epidisplay package ordinal or. The display function obtains the summary statistical information of the ordered logistic regression model (the odds ratio and its confidence interval correspo
- Depth first search - DFS (burst search)
- 如何运营好技术相关的自媒体?
猜你喜欢
2. Create your own NFT collections and publish a Web3 application to show them start and run your local environment
Lumiprobe 生物分子定量丨QuDye 蛋白定量试剂盒
What designs are needed in the architecture to build a general monitoring and alarm platform
Evaluation of 6 red, yellow and black list cameras: who is the safest? Who has good picture quality? From now on, let you no longer step on thunder
Static timing analysis (STA) in ic/fpga design
NSI packaging script add file details
Leetcode203 remove linked list elements
Navicat premium 15 permanent cracking and 2021 latest idea cracking (valid for personal testing)
实现一个Prometheus exporter
12. Design of power divider for ads usage record
随机推荐
Halcon image calibration enables subsequent image processing to become the same as the template image
Static timing analysis (STA) in ic/fpga design
主成分之综合竞争力案例分析
用WPF写一款开源方便、快捷的数据库文档查询、生成工具
Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester
NSI packaging script add file details
After studying 11 kinds of real-time chat software, I found that they all have these functions
docker 部署mysql8.0
解决方案:可以ping别人,但是别人不能ping我
Vidéos courtes recommandées chaque semaine: méfiez - vous de la confusion entre « phénomène » et « problème »
The R language uses the tablestack function of epidisplay package to make statistical summary tables (descriptive statistics based on the grouping of target variables, hypothesis testing, etc.). If th
搭建一个通用监控告警平台,架构上需要有哪些设计
Unity learning fourth week
Roll out! Enlightenment!
1、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》什么是NFT
ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
The ultimate version of the 13th simulation of the single chip microcomputer provincial competition of the Blue Bridge Cup
2、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》启动并运行您的本地环境
ES6数组方法find()、findIndex()的总结「建议收藏」
Localization through custom services in the shuttle application