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();
}
相关推荐
潇-xiao6 分钟前
Qt 验证自动释放 + 乱码问题(6)
c++·笔记·qt
Flower#1 小时前
D. Apple Tree Traversing 【Codeforces Round 1023 (Div. 2)】
c++·算法·图论·dfs
忘梓.2 小时前
从父类到子类:C++ 继承的奇妙旅程(2)
java·开发语言·c++
zxctsclrjjjcph8 小时前
【高并发内存池】从零到一的项目之centralcache整体结构设计及核心实现
开发语言·数据结构·c++·链表
炯哈哈9 小时前
【上位机——MFC】单文档和多文档视图架构
开发语言·c++·mfc·上位机
利刃大大9 小时前
【网络编程】四、守护进程实现 && 前后台作业 && 会话与进程组
linux·网络·c++·网络编程·守护进程
oioihoii10 小时前
C++23 std::tuple与其他元组式对象的兼容 (P2165R4)
c++·链表·c++23
赵和范10 小时前
C++:书架
开发语言·c++·算法
龙湾开发11 小时前
计算机图形学编程(使用OpenGL和C++)(第2版)学习笔记 05.纹理贴图
c++·笔记·学习·3d·图形渲染·贴图
yasuniko12 小时前
C++线程库
开发语言·c++