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();
}
相关推荐
珹洺20 分钟前
C++算法竞赛篇:DevC++ 如何进行debug调试
java·c++·算法
古月-一个C++方向的小白7 小时前
C++11之lambda表达式与包装器
开发语言·c++
tanyongxi669 小时前
C++ AVL树实现详解:平衡二叉搜索树的原理与代码实现
开发语言·c++
斯是 陋室11 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
tju新生代魔迷11 小时前
C++:list
开发语言·c++
HHRL-yx12 小时前
C++网络编程 5.TCP套接字(socket)通信进阶-基于多线程的TCP多客户端通信
网络·c++·tcp/ip
tomato0912 小时前
河南萌新联赛2025第(一)场:河南工业大学(补题)
c++·算法
每一天都要努力^14 小时前
C++拷贝构造
开发语言·c++
NoirSeeker16 小时前
在windows平台上基于OpenHarmony sdk编译三方库并暴露给ArkTS使用(详细)
c++·windows·arkts·鸿蒙·交叉编译
落羽的落羽16 小时前
【C++】(万字)一文看懂“类与对象”
c++