Qt - 自定义进度显示组件

先看效果图,哈哈,我也是初级,大家一起交流交流啊!


170949_q03L_1587794.png

因为Linux系统,喜欢上了Qt,然后又走上了一条不归路。感觉Qt开发窗口应用程序真的不错,尤其是StyleCss的涉及,简直是牛逼!

下面贴代码:

No.1. qcircleprogress.h文件

#ifndef QCIRCLEPROGRESS_H
#define QCIRCLEPROGRESS_H

#include <QWidget>
#include <QPainter>
#include <QPen>
#include <QPixmap>
#include <QRect>
#include <QFont>
#include <QPointF>

class QCircleProgress : public QWidget
{
    Q_OBJECT
public:
    explicit QCircleProgress(QWidget *parent = 0);
    void paintEvent(QPaintEvent *);
    void setPen(QPen pen);
    QPen getPen();
    void setProgressPen(QPen pen);
    QPen getProgressPen();

    int getProgress();
signals:

public slots:
    void setProgress(int value);
private:
    QPainter *painter;
    QPixmap *cacheMap;
    QPen *pen;
    QPen *progressPen;
    int progress;
};

#endif // QCIRCLEPROGRESS_H

No.2. qcircleprogress.cpp文件

#include "qcircleprogress.h"

QCircleProgress::QCircleProgress(QWidget *parent) : QWidget(parent)
{
//    this->setWindowFlags(Qt::FramelessWindowHint);
//    this->setAttribute(Qt::WA_TranslucentBackground);
    this->progress = 0;
    this->setGeometry(0, 0, 300, 300);
    QRect winSize = this->geometry();
    this->cacheMap = new QPixmap(winSize.width(), winSize.height());
    this->painter = new QPainter(cacheMap);
    this->painter->setRenderHint(QPainter::Antialiasing);
//    this->setWindowFlags(Qt::WindowFlags);
}

void QCircleProgress::setPen(QPen pen){
    this->pen = &pen;
    update();
}

QPen QCircleProgress::getPen(){
    return *pen;
}

void QCircleProgress::setProgressPen(QPen pen){
    this->progressPen = &pen;
    update();
}

QPen QCircleProgress::getProgressPen(){
    return *progressPen;
}

void QCircleProgress::setProgress(int value){
    this->progress = value;
    update();
}

int QCircleProgress::getProgress(){
    return progress;
}

void QCircleProgress::paintEvent(QPaintEvent *){
    QPainter painter(this);
    this->cacheMap->fill(Qt::white);
    if(pen != NULL){
        this->painter->setPen(*pen);
        this->painter->drawArc(100, 100, 100, 100, 360 * -8, 360 * 16);
    }
    if(progressPen != NULL){
        this->painter->setPen(*progressPen);
        this->painter->drawArc(100, 100, 100, 100, 4 * 360, int((progress / 100.0f) * 360 * -16.0f));
    }
    QString progressPercent;
    progressPercent.sprintf("%d", progress);
    progressPercent.append("%");
    this->painter->drawText(QRectF(100.0f, 100.0f, 100.0f, 100.0f), progressPercent, QTextOption(Qt::AlignCenter));
    painter.drawPixmap(0, 0, *cacheMap);
}

继续努力,更上一层楼,愿你们一起共勉!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 为什么在头文件中有的是使用前置声明,而有的是包含头文件? 如下代码: 前置声明(forward declarati...
    Joe_HUST阅读 1,483评论 0 6
  • C plus plus Qt 04 - Signals and Slots - YouTube C plus pl...
    静候那一米阳光阅读 554评论 0 1
  • 韩元旭、余橙、沈开洋 Qt介绍 Qt是一个跨平台的C++图形用户界面应用程序框架。它早在1991年奇趣科技公司两位...
    开洋_shen阅读 16,483评论 4 24
  • 亲爱的妈妈啊,别担心。孩儿长大了。在您不经意间,在您不舍得为自己那饱受风霜的脸买一瓶面霜时,悄悄的长大了。 亲爱的...
    巫巫格阅读 460评论 0 2
  • 中午看《釜山行》,又惧又痛,直到现在,人都处于郁郁之中。 电影讲一个倾城悲剧。 因바이오公司生物病毒...
    小禾的故事人生阅读 391评论 0 0

友情链接更多精彩内容