QPushButton的重绘背景(绘制扇形, paintEvent)

新建一个类

#ifndef PIEBUTTON_H
#define PIEBUTTON_H


#include
#include
#include
#include
#include

class pieButton : public QPushButton
{
    Q_OBJECT
public:
    explicit pieButton(QWidget*parent=NULL);
    ~pieButton();
    void updateDraw(int index1 = 0,int index2 = 0,int index3 = 0);
protected:
    void paintEvent(QPaintEvent *e);
    void resizeEvent(QResizeEvent *event);
private:
    QRect m_drawArea;
    QColor drawColor[3];
    QPen drawPen;
    QPen drawTextPen;
    QPoint textPoint[3];
};

#endif // PIEBUTTON_H
#include "piebutton.h"

static QColor standard_Color = QColor(255,0,0);
static QColor empty_Color = QColor(0,0,255);
static QColor unknow_Color = QColor(255,255,255);
static QColor nc_Color = QColor(50,205,50);
static QColor qc_Color = QColor(128,0,128);

pieButton::pieButton(QWidget *parent):QPushButton(parent)
{
    setFlat(true);
    setStyleSheet("QPushButton{background:transparent;}");
    drawColor[0] = QColor(255,255,255);
    drawColor[1] = QColor(255,255,255);
    drawColor[2] = QColor(255,255,255);
    drawPen = QPen(Qt::gray);
    drawTextPen = QPen(Qt::gray);
    textPoint[0] = QPoint(12,32);
    textPoint[1] = QPoint(32,20);
    textPoint[2] = QPoint(32,46);
}

pieButton::~pieButton()
{

}

void pieButton::updateDraw(int index1, int index2, int index3)
{
    switch (index1) {
        case 0:drawColor[0] = unknow_Color;break;
        case 1:drawColor[0] = nc_Color;break;
        case 2:drawColor[0] = qc_Color;break;
        case 3:drawColor[0] = standard_Color;break;
        case 4:drawColor[0] = empty_Color;break;
    }
    switch (index2) {
        case 0:drawColor[1] = unknow_Color;break;
        case 1:drawColor[1] = nc_Color;break;
        case 2:drawColor[1] = qc_Color;break;
        case 3:drawColor[1] = standard_Color;break;
        case 4:drawColor[1] = empty_Color;break;
    }
    switch (index3) {
        case 0:drawColor[2] = unknow_Color;break;
        case 1:drawColor[2] = nc_Color;break;
        case 2:drawColor[2] = qc_Color;break;
        case 3:drawColor[2] = standard_Color;break;
        case 4:drawColor[2] = empty_Color;break;
    }
    if(index1 == 0 && index2 == 0 && index3 == 0){
        drawPen = QPen(Qt::gray);
        drawTextPen = QPen(Qt::gray);
    }else{
        drawPen = QPen(Qt::NoPen);
        drawTextPen = QPen(Qt::gray);
    }
    update();
}

void pieButton::paintEvent(QPaintEvent *e)
{
    QPushButton::paintEvent(e);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);//抗锯齿
    painter.setBrush(QBrush(drawColor[0]));
    painter.setPen(drawPen);
    painter.drawPie(m_drawArea,120*16,120*16);
    painter.setBrush(QBrush(drawColor[1]));
    painter.drawPie(m_drawArea,240*16,120*16);
    painter.setBrush(QBrush(drawColor[2]));
    painter.drawPie(m_drawArea,360*16,120*16);
    painter.setPen(drawTextPen);
    QFont font;
    font.setPointSize(14);
    painter.setFont(font);
    painter.drawText(textPoint[0],"1");
    painter.drawText(textPoint[1],"2");
    painter.drawText(textPoint[2],"3");
}
void pieButton::resizeEvent(QResizeEvent *event)
{
    //以较短的边长作为绘制区域边长
    Q_UNUSED(event);
    m_drawArea = QRect(0,0,width(),height());
    m_drawArea.adjust(5,5,-5,-5);
}

然后在mainwindow.ui中添加两个QPushButton,一个提升为pieButton,另一个做为控制测试

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_2_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_2_clicked()
{
    ui->pushButton->updateDraw(ui->lineEdit->text().toInt(),ui->lineEdit_2->text().toInt(),ui->lineEdit_3->text().toInt());
}

 

你可能感兴趣的:(Qt)