#include "frmplotrect.h" #include "ui_frmplotrect.h" #include "quihelper.h" frmPlotRect::frmPlotRect(QWidget *parent) : QWidget(parent), ui(new Ui::frmPlotRect) { ui->setupUi(this); this->initForm(); } frmPlotRect::~frmPlotRect() { delete ui; } void frmPlotRect::paintEvent(QPaintEvent *) { int width = this->width(); int height = this->height(); QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing); if (type == 0) { QPen pen; pen.setWidth(2); pen.setColor(QColor(200, 0, 0)); painter.setPen(pen); painter.setBrush(Qt::NoBrush); //绘制左侧竖线 painter.drawLine(1, 0, 1, height); //绘制右侧竖线 painter.drawLine(width - 1, 0, width - 1, height); //绘制中间直线 painter.drawLine(0, height / 2, width, height / 2); //绘制中间箭头 QRect textRect(0, 0, width, height / 2 + 5); painter.setPen(QColor(0, 0, 0)); painter.drawText(textRect, Qt::AlignHCenter | Qt::AlignBottom, QChar(0xf106)); } else if (type == 1) { QPen pen; pen.setWidth(4); pen.setColor(QColor(0, 128, 0)); painter.setPen(pen); painter.setBrush(QColor(0, 255, 0, 30)); //绘制矩形区域 painter.drawRect(0, 0, width, height); //绘制圆角矩形区域 //painter.drawRoundedRect(0, 0, width, height, 3, 3); } } void frmPlotRect::initForm() { type = 0; //判断图形字体是否存在,不存在则加入 QFontDatabase fontDb; if (!fontDb.families().contains("FontAwesome")) { int fontId = fontDb.addApplicationFont(":/font/fontawesome-webfont.ttf"); QStringList fontName = fontDb.applicationFontFamilies(fontId); if (fontName.count() == 0) { qDebug() << "load fontawesome-webfont.ttf error"; } } if (fontDb.families().contains("FontAwesome")) { iconFont = QFont("FontAwesome"); #if (QT_VERSION >= QT_VERSION_CHECK(4,8,0)) iconFont.setHintingPreference(QFont::PreferNoHinting); #endif } } void frmPlotRect::setType(int type) { if (this->type != type) { this->type = type; update(); } }