效果图
说明
重写 QWidget 中的 paintEvent() 处理绘图事件,废话不多说,上代码
源码
#ifndef MYWIDGETFORM_H
#define MYWIDGETFORM_H
#include <QWidget>
namespace Ui {
class myWidgetForm;
}
enum MYTYPE{
SIZEWIDTH,
SIZEHEIGHT,
TOPWIDTH,
TOPHEIGHT,
MIDDLEWIDTH,
MIDDLEHEIGHT,
BOTTOMWIDTH,
BOTTOMHEIGHT,
NONE = 10,
};
class myWidgetForm : public QWidget
{
Q_OBJECT
public:
explicit myWidgetForm(QWidget *parent = nullptr);
~myWidgetForm();
void paintEvent(QPaintEvent *event);
void setTopWidth(int topWidth);
void setTopHeight(int topHeight);
void setCenterWidth(int centerWidth);
void setCenterHeight(int centerHeight);
void setBottomWidth(int bottomWidth);
void setBottomHeight(int bottomHeight);
void setZoom(int times, QSize topSize, QSize centerSize, QSize bottomSize);
public slots:
void CrossType(MYTYPE type);
private:
Ui::myWidgetForm *ui;
MYTYPE myType_;
int topWidth_;
int topHeight_;
int centerWidth_;
int centerHeight_;
int bottomWidth_;
int bottomHeight_;
};
#endif // MYWIDGETFORM_H
#include "mywidgetform.h"
#include "ui_mywidgetform.h"
#include <QPainter>
#include <QDebug>
myWidgetForm::myWidgetForm(QWidget *parent)
: QWidget(parent)
, ui(new Ui::myWidgetForm)
{
ui->setupUi(this);
}
myWidgetForm::~myWidgetForm()
{
delete ui;
}
void myWidgetForm::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setBrush(QColor("#000000"));
//原点
int posX = (width() - centerWidth_) / 2;
int posY = (height() - (topHeight_ + centerHeight_ + bottomHeight_)) / 2;
int sizeW = centerWidth_;
int sizeH = topHeight_ + centerHeight_ + bottomHeight_;
//painter.drawRect(posX, posY, sizeW, sizeH);
int leftUpX = (sizeW - topWidth_) / 2;
//十字
painter.drawRect(posX + leftUpX, posY, topWidth_, topHeight_ + centerHeight_ + bottomHeight_);
painter.drawRect(posX, posY + topHeight_, sizeW, centerHeight_ );
//四个遮挡部分
painter.setBrush(Qt::Dense6Pattern);
QRect rect1(posX, posY, leftUpX, topHeight_);
QRect rect2(posX + leftUpX + topWidth_, posY, leftUpX, topHeight_);
QRect rect3(posX, posY + topHeight_ + centerHeight_, leftUpX, bottomHeight_);
QRect rect4(posX + leftUpX + topWidth_, posY + topHeight_ + centerHeight_, leftUpX, bottomHeight_);
painter.drawRect(rect1);
painter.drawRect(rect2);
painter.drawRect(rect3);
painter.drawRect(rect4);
QPen pen;
pen.setWidth(5);
pen.setColor(QColor("#ffffff"));
painter.setPen(pen);
//左上
painter.drawLine(posX + leftUpX, posY + 0, posX + leftUpX, posY + topHeight_);
painter.drawLine(posX + 0, posY + topHeight_, posX + leftUpX, posY + topHeight_);
//右上
painter.drawLine(posX + centerWidth_, posY + topHeight_, posX + topWidth_ + leftUpX, posY + topHeight_);
painter.drawLine(posX + topWidth_ + leftUpX, posY + 0, posX + topWidth_ + leftUpX, posY + topHeight_);
//左下
painter.drawLine(posX + 0, posY + centerHeight_ + topHeight_, posX + leftUpX, posY + centerHeight_ + topHeight_);
painter.drawLine(posX + leftUpX, posY + sizeH, posX + leftUpX, posY + centerHeight_ + topHeight_);
//右下
painter.drawLine(posX + centerWidth_, posY + topHeight_ + centerHeight_, posX + topWidth_ + leftUpX, posY + topHeight_ + centerHeight_);
painter.drawLine(posX + topWidth_ + leftUpX, posY + sizeH, posX + topWidth_ + leftUpX, posY + topHeight_ + centerHeight_);
//宽高
painter.drawLine(posX + 0, posY + 0, posX + sizeW, posY + 0);
painter.drawLine(posX + 0, posY + 0, posX + 0, posY + sizeH);
pen.setWidth(10);
pen.setColor(QColor("#ffff00"));
painter.setPen(pen);
switch(myType_) {
case SIZEWIDTH:
painter.drawLine(posX + 0, posY + 0, posX + sizeW, posY + 0);
break;
case SIZEHEIGHT:
painter.drawLine(posX + 0, posY + 0, posX + 0, posY + sizeH);
break;
case TOPWIDTH:
painter.drawLine(posX + leftUpX, posY + 0, posX + leftUpX + topWidth_, posY + 0);
break;
case TOPHEIGHT:
painter.drawLine(posX + leftUpX, posY + 0, posX + leftUpX, posY + topHeight_);
break;
case MIDDLEWIDTH:
painter.drawLine(posX + 0, posY + topHeight_, posX + sizeW, posY + topHeight_);
break;
case MIDDLEHEIGHT:
painter.drawLine(posX + 0, posY + topHeight_, posX + 0, posY + topHeight_ + centerHeight_);
break;
case BOTTOMWIDTH:
painter.drawLine(posX + leftUpX, posY + topHeight_ + centerHeight_, posX + leftUpX + topWidth_, posY + topHeight_ + centerHeight_);
break;
case BOTTOMHEIGHT:
painter.drawLine(posX + leftUpX, posY + topHeight_ + centerHeight_, posX + leftUpX, posY + sizeH);
break;
default:
break;
}
painter.end();
update();
}
void myWidgetForm::setTopWidth(int topWidth)
{
topWidth_ = topWidth;
}
void myWidgetForm::setTopHeight(int topHeight)
{
topHeight_ = topHeight;
}
void myWidgetForm::setCenterWidth(int centerWidth)
{
centerWidth_ = centerWidth;
}
void myWidgetForm::setCenterHeight(int centerHeight)
{
centerHeight_ = centerHeight;
}
void myWidgetForm::setBottomWidth(int bottomWidth)
{
bottomWidth_ = bottomWidth;
}
void myWidgetForm::setBottomHeight(int bottomHeight)
{
bottomHeight_ = bottomHeight;
}
void myWidgetForm::setZoom(int times, QSize topSize, QSize centerSize, QSize bottomSize)
{
topWidth_ = topSize.width() * times;
topHeight_ = topSize.height() * times;
centerWidth_ = centerSize.width() * times;
centerHeight_ = centerSize.height() * times;
bottomWidth_ = bottomSize.width() * times;
bottomHeight_ = bottomSize.height() * times;
}
void myWidgetForm::CrossType(MYTYPE type)
{
myType_ = type;
}