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

QT-异步编程

1.进度条通过线程动起来

mythread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QSlider>
class myThread : public QThread
{
    Q_OBJECT
public:
    myThread(QObject* parent=nullptr);
protected:
    virtual void run() override;
private:

signals:
    emit void sendMove();
};

#endif // MYTHREAD_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H
#include "mythread.h"
#include <QWidget>

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;
    QString qss;
public slots:
    void recMove();
private slots:
    void on_pushButton_clicked();
};
#endif // WIDGET_H

mythread.cpp

#include "mythread.h"

myThread::myThread(QObject* parent)
    :QThread(parent)
{


}

void myThread::run()
{
    while(1)
    {
        this->msleep(100);
        emit sendMove();
    }
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    thread = new myThread(this);
    qss ="QSlider::sub-page{background-color:blue} QSlider::groove{background-color:black} ";
    ui->horizontalSlider->setStyleSheet(qss);
    QObject::connect(thread,&myThread::sendMove,this,&Widget::recMove);//传递滑块移动
}

Widget::~Widget()
{
    thread->terminate();
    delete ui;
}


void Widget::recMove()
{
    int currentValue = ui->horizontalSlider->value();
    ui->horizontalSlider->setValue(currentValue + 1);
}


void Widget::on_pushButton_clicked()
{
    thread->start();
}

2.使用QFileDialog 或者 拖放事件 + QT文件IO + QT线程

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

额外要求:可以在文件拷贝的时候,追加一个进度条显示拷贝了多少文件内容

mythread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H
 
#include <QThread>
#include <QFile>
 
class myThread : public QThread
{
    Q_OBJECT
public:
    explicit myThread(QObject* parent = nullptr);
    void setFileInfo(const QString& path, const QString& copypath);
 
signals:
    void signCopyIng(int value);
    void signCopyEnd(bool success);
 
protected:
    void run() override;
 
private:
    QString path;   // 源文件路径
    QString copypath;  // 目标文件路径
};
 
#endif // MYTHREAD_H

mythread.cpp

#include "mythread.h"
#include <QDebug>
 
myThread::myThread(QObject *parent) : QThread(parent) {}
 
void myThread::setFileInfo(const QString& path, const QString& copypath) {
    this->path = path;
    this->copypath = copypath;
}
 
void myThread::run() {
    QFile srcFile(path);
    QFile destFile(copypath);
 
    if (!srcFile.open(QIODevice::ReadOnly)) {
        emit signCopyEnd(false);
        return;
    }
 
    if (!destFile.open(QIODevice::WriteOnly)) {
        srcFile.close();
        emit signCopyEnd(false);
        return;
    }
 
    const qint64 totalSize = srcFile.size();
    qint64 copiedSize = 0;
    const int bufferSize = 1024 * 1024;
 
    while (!srcFile.atEnd()) {
        if (isInterruptionRequested()) break;
 
        QByteArray buffer = srcFile.read(bufferSize);
        qint64 writeSize = destFile.write(buffer);
 
        if (writeSize != buffer.size()) {
            break;
        }
 
        copiedSize += writeSize;
        int value = static_cast<int>(copiedSize * 100 / totalSize);
        emit signCopyIng(value);
    }
 
    srcFile.close();
    destFile.close();
 
    bool success = (copiedSize == totalSize);
    emit signCopyEnd(success);
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H
 
#include <QWidget>
#include "mythread.h"
#include <QTimer>
 
 
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 onCopyIng(int value);
    void onCopyEnd(bool success);
 
    void on_pushButton_clicked();
};
 
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QMessageBox>
 
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , thread(new myThread(this))
{
    ui->setupUi(this);
 
 
    QString qss ="QSlider::sub-page{background-color:blue} QSlider::groove{background-color:black} ";
      
 
    ui->horizontalSlider->setEnabled(false); // 禁用进度条交互
    ui->horizontalSlider->setStyleSheet(qss);
 
    ui->horizontalSlider->setRange(0, 100);
    ui->horizontalSlider->setValue(0);
    ui->label->setText("就绪");
 
    connect(thread, &myThread::signCopyIng, this, &Widget::onCopyIng);
    connect(thread, &myThread::signCopyEnd, this, &Widget::onCopyEnd);
}
 
Widget::~Widget()
{
    if (thread->isRunning()) {
        thread->requestInterruption();
        thread->wait();
    }
    delete ui;
}
 
void Widget::on_pushButton_clicked()
{
    QString path = QFileDialog::getOpenFileName(this, "选择源文件");
    if (path.isEmpty()) return;
 
    QString copypath = QFileDialog::getSaveFileName(this, "保存文件");
    if (copypath.isEmpty()) return;
 
    ui->horizontalSlider->setValue(0);
    ui->label->setText("复制中...0%");
    ui->pushButton->setEnabled(false);
 
    thread->setFileInfo(path, copypath);
 
    thread->start();
}
 
void Widget::onCopyIng(int value)
{
    ui->horizontalSlider->setValue(value);
    ui->label->setText(QString("复制中...%1%").arg(value));
}
 
void Widget::onCopyEnd(bool success)
{
    ui->pushButton->setEnabled(true);
    if (success) {
        QMessageBox::information(this, "完成", "文件复制成功!");
        ui->label->setText("复制完成");
    } else {
        QMessageBox::critical(this, "错误", "文件复制失败!");
        ui->label->setText("复制失败");
    }
    ui->horizontalSlider->setValue(0);
}


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

相关文章:

  • Kafka,Mq,Redis作为消息队列使用时的差异?|消息队列
  • 硬通货用Deekseek做一个Vue.js组件开发的教程
  • GCC RISCV 后端 -- C语言语法分析过程
  • C# OnnxRuntime部署DAMO-YOLO人头检测
  • 《基于WebGPU的下一代科学可视化——告别WebGL性能桎梏》
  • Unity 小功能
  • Linux 开发工具
  • 【微知】Mellanox驱动中to是什么?有哪些超时时间?(time out,心跳2s,reset 1分钟)
  • Docker的常用镜像
  • 如何将一台服务器的pip环境迁移到另一个机器?
  • MoE 架构:专家齐聚,智启未来 —— 解锁传统稠密模型的瓶颈
  • 某书x-s参数更新自动化获取密钥iv脚本
  • 网页制作11-html,css,javascript初认识のCCS样式列表(下)
  • Go学习笔记:基础语法6
  • 庭田科技携手西门子工业软件成功举办振动噪声技术研讨会
  • 从“0”开始入门PCB之(5)完结篇!--快速入门原理图DRC,PCB的符号与布局,2D与3D效果,PCB的图层和布线,PCB板框
  • 攻防世界WEB(新手模式)19-file_include
  • 机器视觉运动控制一体机在天地盖同步跟随贴合解决方案
  • 三数之和~
  • 快手,蓝禾,得物,优博讯,三七互娱,顺丰,oppo,游卡,汤臣倍健,康冠科技,作业帮25届春招内推