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();
}
相关推荐
AI+程序员在路上12 分钟前
Qt6中模态与非模态对话框区别
开发语言·c++·qt
岁忧5 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
蜉蝣之翼❉10 小时前
CRT 不同会导致 fopen 地址不同
c++·mfc
aramae10 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
lixzest11 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
_Chipen12 小时前
C++基础问题
开发语言·c++
灿烂阳光g12 小时前
OpenGL 2. 着色器
c++·opengl
AA陈超13 小时前
虚幻引擎UE5专用服务器游戏开发-20 添加基础能力类与连招能力
c++·游戏·ue5·游戏引擎·虚幻
mit6.82413 小时前
[Meetily后端框架] AI摘要结构化 | `SummaryResponse`模型 | Pydantic库 | vs marshmallow库
c++·人工智能·后端
R-G-B13 小时前
【02】MFC入门到精通——MFC 手动添加创建新的对话框模板
c++·mfc·mfc 手动添加创建新的对话框