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);
}

繼續努力,更上一層樓,愿你們一起共勉!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 為什么在頭文件中有的是使用前置聲明,而有的是包含頭文件? 如下代碼: 前置聲明(forward declarati...
    Joe_HUST閱讀 1,313評論 0 6
  • C plus plus Qt 04 - Signals and Slots - YouTube C plus pl...
    靜候那一米陽光閱讀 475評論 0 1
  • 韓元旭、余橙、沈開洋 Qt介紹 Qt是一個跨平臺的C++圖形用戶界面應用程序框架。它早在1991年奇趣科技公司兩位...
    開洋_shen閱讀 16,208評論 4 24
  • 親愛的媽媽啊,別擔心。孩兒長大了。在您不經意間,在您不舍得為自己那飽受風霜的臉買一瓶面霜時,悄悄的長大了。 親愛的...
    巫巫格閱讀 371評論 0 2
  • 中午看《釜山行》,又懼又痛,直到現在,人都處于郁郁之中。 電影講一個傾城悲劇。 因???公司生物病毒...
    小禾的故事人生閱讀 266評論 0 0