QTday4

1:是进度条通过线程自己动起来

mythread.h

复制代码
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>

class mythread : public QThread
{
    Q_OBJECT
public:
    mythread(QObject* parent = nullptr);
protected:
    virtual void run() override;
private:
signals:
    virtual void sndSignal(int value);
};

#endif // MYTHREAD_H

widget.h

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSlider>
#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 slots:
    void on_pushButton_clicked();
    void rcvSig(int value);
private:
    Ui::Widget *ui;
    mythread* thread;
    QSlider* slider;
};
#endif // WIDGET_H

mythread.cpp

复制代码
#include "mythread.h"

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

}


void mythread::run()
{
    int value = 0;
    while(1)
    {
        this->msleep(100);
        emit sndSignal(value);
        value++;
        if(value == 100)
        {
            value = 0;
        }
    }
}

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);
    QString qss = R"(
           QSlider::groove:horizontal {
               background-color: #f0f0f0;  /* 滑轨的背景色为灰白色 */
               height: 10px;  /* 滑轨的高度增大 */
               border-radius: 5px;  /* 滑轨的圆角 */
           }

           QSlider::sub-page:horizontal {
               background-color: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #6a11cb, stop:1 #2575fc);  /* 滑块左侧的渐变背景色 */
               height: 10px;  /* 滑块左侧的高度增大 */
               border-radius: 5px;  /* 滑块左侧的圆角 */
           }

           QSlider::handle:horizontal {

               width: 24px;  /* 滑块手柄的宽度增大 */
               height: 24px;  /* 滑块手柄的高度增大 */
               margin: -7px 0;  /* 滑块手柄的垂直居中调整 */
               border-radius: 12px;  /* 滑块手柄的圆角增大 */

           }
       )";
    ui->horizontalSlider->setStyleSheet(qss);




    QObject::connect(thread,&mythread::sndSignal,this,&Widget::rcvSig);
    thread->start();
}

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



void Widget::rcvSig(int value)
{
    ui->horizontalSlider->setValue(value);
}

2:使用QFileDialog或者拖放事件+QT文件IO+QT线程
实现一个文件复制功能,要求能够复制大小超过800mb的文件
额外要求:可以在文件拷贝的时候,追加一个进度条显示拷贝了多少文件内容

mythread.h

复制代码
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include <QFile>

class myThread : public QThread
{
    Q_OBJECT
private:
    QFile file1;
    QFile file2;
    QString filename;
    QString newfilename;
public:
    myThread(QObject* parent);
    void setFile(const QString& filename);
    void setNewFile(const QString& newfilename);
protected:
    virtual void run() override;
};

#endif // MYTHREAD_H

widget.h

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QFileDialog>
#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 slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

private:
    Ui::Widget *ui;
    QString filename;
    QString newfilename;
    myThread* thread;
};
#endif // WIDGET_H

mythread.cpp

复制代码
#include "mythread.h"

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

}

void myThread::setFile(const QString &filename)
{
    this->filename = filename;
}

void myThread::setNewFile(const QString &newfilename)
{
    this->newfilename = newfilename;
}

void myThread::run()
{
    file1.setFileName(filename);
    file1.open(QIODevice::ReadOnly);

    file2.setFileName(newfilename);
    file2.open(QIODevice::WriteOnly);
    while(1)
    {
        QByteArray arr = file1.read(1);
        if(arr.isEmpty())
        {
            break;
        }
       file2.write(arr);
    }

}

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

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    filename = QFileDialog::getOpenFileName(this,"选择文件");
    ui->lineEdit->setText(filename);
    thread->setFile(filename);
}

void Widget::on_pushButton_2_clicked()
{
    newfilename = QFileDialog::getSaveFileName(this,"选择保存的地址");
    ui->lineEdit_2->setText(newfilename);
    thread->setNewFile(newfilename);
}

void Widget::on_pushButton_3_clicked()
{
    thread->start();
}
相关推荐
Murphy_lx5 小时前
Lambda表达式
开发语言·c++
yangpipi-5 小时前
C++并发编程-23. 线程间切分任务的方法
开发语言·c++
楼田莉子6 小时前
C++算法专题学习——分治
数据结构·c++·学习·算法·leetcode·排序算法
ulias2127 小时前
各种背包问题简述
数据结构·c++·算法·动态规划
程序喵大人7 小时前
分享个C++线程池的实现源码
开发语言·c++·线程池
FL16238631297 小时前
[ubuntu][C++]onnxruntime安装cpu版本后测试代码
linux·c++·ubuntu
要做朋鱼燕7 小时前
【C++】 priority_queue 容器模拟实现解析
开发语言·c++·笔记·职场和发展
励志不掉头发的内向程序员8 小时前
C++进阶——继承 (1)
开发语言·c++·学习
mit6.82410 小时前
并查集|栈
c++
中国胖子风清扬10 小时前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust