当前位置:网站首页>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
边栏推荐
猜你喜欢
主成分之综合竞争力案例分析
Lumiprobe non fluorescent alkyne EU (5-ethynyluridine)
What if the reliability coefficient is low? How to calculate the reliability coefficient?
Localization through custom services in the shuttle application
C# SelfHost WebAPI (2)
Lumiprobe lumizol RNA extraction reagent solution
How does factor analysis calculate weights?
如何在自有APP内实现小程序实现连麦直播
Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
随机推荐
Livedata postvalue will "lose" data
Halcon图片标定,使得后续图片处理过后变成与模板图片一样
How to manage 1000 anchors by one person?
Solution: you can ping others, but others can't ping me
R语言使用epiDisplay包的tableStack函数制作统计汇总表格(基于目标变量分组的描述性统计、假设检验等)、不设置by参数则计算数据框指定数据列范围的基础描述性统计信息
R语言ggplot2可视化:gganimate包transition_time函数创建动态散点图动画(gif)、shadow_wake函数配置动画的渐变效果(gradual falloff)拖尾效应
用GSConv+Slim Neck改进Yolov5,将性能提升到极致!
Leetcode-160相交链表
Using OpenSSL encryption to rebound shell traffic
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
Search 2D matrix 2
[Chongqing Guangdong education] basic psychology reference materials of Tianjin Normal University
Mysql database of easyclick
How to change guns for 2D characters
华为云专家详解GaussDB(for MySQL)新特性
Implement a Prometheus exporter
2、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》启动并运行您的本地环境
Leetcode203 移除链表元素
R language ggplot2 visualization: visualize the line graph and add customized Y-axis label information to the line graph using the labels function
用WPF写一款开源方便、快捷的数据库文档查询、生成工具