当前位置: 首页 > article >正文

【无标题】QT相关练习

 二、

  1. 使用 QFileDialog  + QT文件IO + QT线程。

  2. 实现一个文件复制功能,要求能够复制大小超过800MB的文件。

  3. 额外要求:在文件拷贝时,追加一个进度条显示拷贝了多少文件内容。

程序代码:

<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();
}

运行结果:


http://www.kler.cn/a/600853.html

相关文章:

  • 框架的CVE漏洞利用 php类 java类 手工操作和自动化操作蓝队分析漏洞利用的流量特征
  • 力扣题(3):寻找两个正序数组的中位数
  • 纯HTML+CSS实现3D空间正方体旋转
  • Opencv计算机视觉编程攻略-第三节 图像颜色处理
  • 23种设计模式-中介者(Mediator)设计模式
  • 网关助力冶金厂电表数据高效采集与监控
  • 【Redis】redis实现消息的发布和订阅
  • 使用python爬取网络资源
  • sendRedirect()和forward()方法有什么区别?
  • 嵌入式系统应用-拓展-STM32 低功耗设计
  • unittest自动化测试实战
  • 【ESP32S3】esp32获取串口数据并通过http上传到前端
  • Docker 搭建 PlantUML 服务:高效生成 UML 图的最佳实践
  • 36.评论日记
  • QT原子变量:QAtomicInteger、QAtomicPointer、QAtomicFlag
  • Win11 环境使用WSL安装Ubunut系统
  • winstart.wsf 病毒清理大作战
  • “需求引致供给“在互联网时代的范式重构:基于开源AI智能名片链动2+1模式S2B2C商城小程序源码的技术经济学分析
  • 计算机网络 --应用层
  • 【设计模式】深入解析装饰器模式(Decorator Pattern)