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();
}
相关推荐
HellowAmy3 小时前
我的C++规范 - 玩一个小游戏
开发语言·c++·代码规范
自学不成才3 小时前
深度复盘:一次flutter应用基于内存取证的黑盒加密破解实录并完善算法推理助手
c++·python·算法·数据挖掘
玖釉-5 小时前
[Vulkan 学习之路] 08 - 给图片穿马甲:图像视图 (Image Views)
c++·windows·图形渲染
m0_748250036 小时前
C++ 信号处理
c++·算法·信号处理
yuyanjingtao6 小时前
动态规划 背包 之 凑钱
c++·算法·青少年编程·动态规划·gesp·csp-j/s
scx201310047 小时前
20260112树状数组总结
数据结构·c++·算法·树状数组
星竹晨L7 小时前
【C++内存安全管理】智能指针的使用和原理
开发语言·c++
智者知已应修善业8 小时前
【C语言 dfs算法 十四届蓝桥杯 D飞机降落问题】2024-4-12
c语言·c++·经验分享·笔记·算法·蓝桥杯·深度优先
玖釉-8 小时前
[Vulkan 学习之路] 09 - 显卡的流水线工厂:图形管线概览 (Graphics Pipeline)
c++·windows·图形渲染
无限进步_9 小时前
【C语言&数据结构】二叉树遍历:从前序构建到中序输出
c语言·开发语言·数据结构·c++·算法·github·visual studio