美文网首页Qt学习
Qt - 自定义进度显示组件

Qt - 自定义进度显示组件

作者: Cosecant | 来源:发表于2017-04-13 16:19 被阅读89次

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


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

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

相关文章

网友评论

    本文标题:Qt - 自定义进度显示组件

    本文链接:https://www.haomeiwen.com/subject/oqgbattx.html