本文为学习记录,若有不足,请联系作者,谦虚受教。
前言
有些烦恼,丢掉了,才有云淡风轻的机会。一、.h文件
#include<QFile>
#include<QTextStream>
#include<QFileDialog>
// QTextStream是一个非常强大的类,它可以对IO设备,QString,QByteArray等类进行方便的读写操作
//QFileDialog 是Qt 中用于文件打开和保存的对话框
private slots:
void on_openfilebutton_clicked();
void on_sendfilebutton_clicked();
void on_stopfilebutton_clicked();
二、.cpp文件
/**********************************************
* 打开文件、发送文件、停止发送
**********************************************/
//打开文件
void MainWindow::on_openfilebutton_clicked()
{
QString filename=QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
//一直使用QDir::currentPath()来获取程序所在绝对路径,根据路径在指定目录创建合适的文件夹。
ui->filepathlineEdit->clear();
ui->filepathlineEdit->setText(filename);
QFile file(filename);
ui->sendtextEdit->append("\n");
if(!file.exists())
{
QMessageBox::information(this,"警告","文件不存在!");
return;
}
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::information(this,"警告","文件打不开!");
return;
}
while(!file.atEnd()){
binByteArray.append(file.readLine(512));
}
file.close();
QMessageBox::information(this,"操作","打开文件成功!");
}
//发送文件
void MainWindow::on_sendfilebutton_clicked()
{
serial->write(binByteArray);
QMessageBox::information(this,"操作","发送成功!");
updatesendfileprogress();
}
//停止发送文件
void MainWindow::on_stopfilebutton_clicked()
{
serial->close();
QMessageBox::information(this,"操作","停止发送成功!");
}
总结
善于总结,多进一步。解不开的心结就把它系成蝴蝶结吧。