【无标题】QT相关练习
二、
使用
QFileDialog
+ QT文件IO + QT线程。实现一个文件复制功能,要求能够复制大小超过800MB的文件。
额外要求:在文件拷贝时,追加一个进度条显示拷贝了多少文件内容。
程序代码:
<1> Widget.h:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QThread>
#include "mythread.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
myThread* thread;
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
};
#endif // WIDGET_H
<2> Widget.cpp:
#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
thread = new myThread(this);
connect(thread, &myThread::pupdate, ui->horizontalSlider, &QSlider::setValue);
QString qss = (R"(
QSlider {
background-color: transparent;
}
QSlider::groove:horizontal {
background-color: #E0E0E0;
height: 10px;
border-radius: 5px;
}
QSlider::sub-page:horizontal {
background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #6A11CB, stop:1 #2575FC);
border-radius: 5px;
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
}
QSlider::handle:horizontal {
background: #FFFFFF;
width: 16px;
height: 16px;
margin: -4px 0;
border-radius: 8px;
border: 2px solid #2575FC;
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
}
)");
ui->horizontalSlider->setStyleSheet(qss);
}
Widget::~Widget()
{
thread->terminate();
delete ui;
}
void Widget::on_pushButton_clicked()
{
QString sourceFilePath = QFileDialog::getOpenFileName(this, "Select Source File");
if (sourceFilePath.isEmpty()) {
return;
}
QString destinationFilePath = QFileDialog::getSaveFileName(this, "Select Destination File");
if (destinationFilePath.isEmpty()) {
return;
}
thread->setFilePaths(sourceFilePath, destinationFilePath);
thread->start();
}
void Widget::on_pushButton_2_clicked()
{
thread->terminate();
}
<3> myThread.h:
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QFile>
class myThread : public QThread
{
Q_OBJECT
public:
myThread(QObject* parent = nullptr);
void setFilePaths(const QString &source, const QString &destination);
protected:
virtual void run() override;
signals:
void pupdate(int progress);
private:
QString sourcePath;
QString destinationPath;
};
#endif // MYTHREAD_H
<4> myThread.cpp:
#include "mythread.h"
#include <QFile>
#include <QDebug>
myThread::myThread(QObject* parent)
: QThread(parent)
{
}
void myThread::setFilePaths(const QString &source, const QString &destination)
{
sourcePath = source;
destinationPath = destination;
}
void myThread::run()
{
QFile sourceFile(sourcePath);
QFile destinationFile(destinationPath);
if (!sourceFile.open(QIODevice::ReadOnly)) {
qDebug() << "Failed to open source file";
return;
}
if (!destinationFile.open(QIODevice::WriteOnly)) {
qDebug() << "Failed to open destination file";
sourceFile.close();
return;
}
qint64 fileSize = sourceFile.size();
qint64 totalBytesRead = 0;
char buffer[4096];
while (!sourceFile.atEnd()) {
qint64 bytesRead = sourceFile.read(buffer, sizeof(buffer));
destinationFile.write(buffer, bytesRead);
totalBytesRead += bytesRead;
int progress = static_cast<int>((totalBytesRead * 100) / fileSize);
emit pupdate(progress);
}
sourceFile.close();
destinationFile.close();
}
运行结果: