自定义一个简易的Qt画板类

BoxPainter.h

#ifndef BOXPAINTER_H
#define BOXPAINTER_H

#include
#include
#include
#include
using namespace std;
class BoxPainter:public QLabel
{
    Q_OBJECT
public:
    explicit BoxPainter(QWidget *parent = 0);

    ~BoxPainter(){}

    void addRect(int x, int y, int w, int h, int angel=0);

    void addRect(QRect rect, int angel=0);

private:
    vector angels;
    vector rects;

    void paintEvent(QPaintEvent *e);
};


#endif // BOXPAINTER_H

BoxPainter.cpp

#include "BoxPainter.h"
#include 

BoxPainter::BoxPainter(QWidget *parent): QLabel(parent)
{
    this->setStyleSheet("background-color:white");
    rects.clear();
    angels.clear();
}

void BoxPainter::addRect(int x, int y, int w, int h, int angel)
{
    QRect rect(x,y,w,h);
    rects.push_back(rect);
    angels.push_back(angel);
}

void BoxPainter::addRect(QRect rect, int angel)
{
    rects.push_back(rect);
    angels.push_back(angel);
}

void BoxPainter::paintEvent(QPaintEvent *e)
{
    QWidget::paintEvent(e);
    QPainter painter(this);

    // 反走样
    painter.setRenderHint(QPainter::Antialiasing, true);
    // 设置画笔颜色、宽度
   // painter.setPen(QPen(QColor(0, 160, 230), 2));
    // 设置画刷颜色
  //  painter.setBrush(QColor(255, 160, 90));
    painter.setFont(QFont("Monaco",10));
    for(int i = 0; i

待完善中~~~~~~~~

你可能感兴趣的:(Qt)