qt上传文件到iis服务器,zzssdd2:QT串口助手(五):文件操作

/*

函 数:on_Send_Bt_clicked

描 述:发送按键点击信号槽

输 入:无

输 出:无

*/

void Widget::on_Send_Bt_clicked()

{

if (isSerialOpen != false)

{

if (isSendFile)//发送文件数据

{

if (ui->Send_Bt->text() == "发送")

{

ui->Send_Bt->setText("停止");

SendFile();

}

else

{

ui->Send_Bt->setText("发送");

FileSendTimer->stop();

}

}

else//发送发送框数据

{

SerialSendData(SendTextEditBa);

}

}

else

{

QMessageBox::information(this, "提示", "串口未打开");

}

}

/*

函 数:SendData

描 述:定时器发送文件

输 入:无

输 出:无

*/

void Widget::SendFile(void)

{

/*按设置参数发送*/

FrameLen = ui->PackLen_lineEdit->text().toInt(); // 帧大小

FrameGap = ui->GapTim_lineEdit->text().toInt(); // 帧间隔

int textlen = Widget::FileText.size(); // 文件大小

if (FrameGap <= 0 || textlen <= FrameLen)

{

//时间间隔为0 或 帧大小≥文件大小 则直接一次发送

serial->write(FileText.toUtf8());

ui->Send_Bt->setText("发送");

}

else

{

//按设定时间和长度发送

FrameNumber = textlen / FrameLen; // 包数量

LastFrameLen = textlen % FrameLen; // 最后一包数据长度

//进度条步进值

if (FrameNumber >= 100)

{

ProgressBarStep = FrameNumber / 100;

}

else

{

ProgressBarStep = 100 / FrameNumber;

}

//设置定时器

FileSendTimer->start(FrameGap);

}

}

设置一个定时器,将文件按照设定的帧大小和帧间隔来发送:

/*文件帧发送定时器信号槽*/

FileSendTimer = new QTimer(this);

connect(FileSendTimer,SIGNAL(timeout()),this,SLOT(File_TimerSend()));

/*

函 数:File_TimerSend

描 述:发送文件定时器槽函数

输 入:无

输 出:无

*/

void Widget::File_TimerSend(void)

{

if (FrameCount < FrameNumber)

{

serial->write(FileText.mid(FrameCount * FrameLen, FrameLen).toUtf8());

FrameCount++;

//更新进度条

ui->progressBar->setValue(ProgressBarValue += ProgressBarStep);

}

else

{

if (LastFrameLen > 0)

{

serial->write(FileText.mid(FrameCount * FrameLen, LastFrameLen).toUtf8());

ui->progressBar->setValue(100);

}

/*发送完毕*/

FileSendTimer->stop();

FrameCount = 0;

ProgressBarValue = 0;

FrameNumber = 0;

LastFrameLen = 0;

QMessageBox::information(this, "提示", "发送完成");

ui->progressBar->setValue(0);

ui->Send_Bt->setText("发送");

}

}

QString QString::mid(int position, int n = -1) const

Returns a string that contains n characters of this string, starting at the specified position index.

Returns a null string if the position index exceeds the length of the string. If there are less than n characters available in the string starting at the given position, or if n is -1 (default), the function returns all characters that are available from the specified position.

Example:

QString x = "Nine pineapples";

QString y = x.mid(5, 4); // y == "pine"

QString z = x.mid(5); // z == "pineapples"

2.5、数据保存

当保存数据按钮按下时,将接收框数据按文本方式进行保存:

/*

函 数:on_SaveData_Button_clicked

描 述:保存数据按钮点击槽函数

输 入:无

输 出:无

*/

void Widget::on_SaveData_Button_clicked()

{

QString data = ui->Receive_TextEdit->toPlainText();

if (data.isEmpty())

{

QMessageBox::information(this, "提示", "数据内容空");

return;

}

QString curPath = QDir::currentPath(); //获取系统当前目录

QString dlgTitle = "保存文件"; //对话框标题

QString filter = "文本文件(*.txt);;所有文件(*.*)"; //文件过滤器

QString filename = QFileDialog::getSaveFileName(this,dlgTitle,curPath,filter);

if (filename.isEmpty())

{

return;

}

QFile file(filename);

if (!file.open(QIODevice::WriteOnly))

{

return;

}

/*保存文件*/

QTextStream stream(&file);

stream << data;

file.close();

}

QTextStream::QTextStream(FILE **fileHandle, QIODevice::OpenModeopenMode* = QIODevice::ReadWrite)

Constructs a QTextStream that operates on fileHandle, using openMode to define the open mode. Internally, a QFile is created to handle the FILE pointer.

This constructor is useful for working directly with the common FILE based input and output streams: stdin, stdout and stderr. Example:

QString str;

QTextStream in(stdin);

in >> str;

三、扩展

这里对接收模块的功能进行一个补充。上面说了应用程序默认编码是UTF-8,那么如果在ascii显示模式下收到的数据是GBK格式的编码就会造成显示乱码,所以需要对接收数据进行编码判断,如果是GBK编码则进行转换显示。

/*

函 数:SerialPortReadyRead_slot

描 述:readyRead()信号对应的数据接收槽函数

输 入:无

输 出:无

*/

void Widget::SerialPortReadyRead_slot()

{

/*读取串口收到的数据*/

QByteArray bytedata = serial->readAll();

//......省略

if(ui->HexDisp_checkBox->isChecked() == false) //ascii显示

{

/* 判断编码 */

QTextCodec::ConverterState state;

//调用utf8转unicode的方式转码,获取转换结果

QString asciidata = QTextCodec::codecForName("UTF-8")->toUnicode(bytedata.constData(),bytedata.size(),&state);

//存在无效字符则是GBK,转码后返回

if (state.invalidChars > 0)

{

asciidata = QTextCodec::codecForName("GBK")->toUnicode(bytedata);

}

else

{

asciidata = QString(bytedata);

}

//......省略

}

//......省略

}

070e75ad894f645dd7c195a52f9d6a42.png

四、总结

本章主要讲解对文件的操作。除了需要了解在QT中对文件类的常用操作之外,个人认为还有比较重要的两个知识点:1是文本编码的判断和转码处理,2是对于二进制文本的读取。bk

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
错误信息 "./imx283_test: error while loading shared libraries: libtiff.so.6: cannot open shared object file: No such file or directory" 表明在执行"./imx283_test"时,系统无法找到名为"libtiff.so.6"的共享库文件。 为了解决这个问题,您可以尝试以下几个步骤: 1. 确认共享库文件是否存在:可以使用命令"ls /path/to/libtiff.so.6"来检查"/path/to/libtiff.so.6"是否存在。如果文件不存在,则需要安装或重新编译相应的库文件。 2. 添加共享库文件路径:如果共享库文件存在于非标准的库文件路径中,您需要将该路径添加到系统的共享库文件搜索路径中。可以通过设置LD_LIBRARY_PATH环境变量来实现。例如,使用命令"export LD_LIBRARY_PATH=/path/to/library"将路径"/path/to/library"添加到库文件搜索路径中。 3. 更新共享库缓存:在某些情况下,系统可能需要更新共享库缓存以使新安装的库文件可用。您可以使用"sudo ldconfig"命令来更新共享库缓存。 请注意,具体的解决方法可能会因系统配置和软件环境的不同而有所差异。建议您根据实际情况选择适合的解决方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [STM32MP157移植Qt5.12.10](https://blog.csdn.net/zzssdd2/article/details/123310117)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [tslib1.4移植,Qt4.8.7移植arm,qt5.7.0移植arm](https://blog.csdn.net/qq1113231395/article/details/84392552)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值