51 lines
1.4 KiB
C++
51 lines
1.4 KiB
C++
#include <QPainter>
|
|
#include "graphicsItem/itemExtend.h"
|
|
|
|
ItemExtend::ItemExtend(QGraphicsItem *parent)
|
|
: HandleRect(parent)
|
|
{
|
|
setAcceptHoverEvents(true);
|
|
setFlag(QGraphicsItem::ItemIsSelectable, true);
|
|
setFlag(QGraphicsItem::ItemIsFocusable, true);
|
|
m_penWidth = 2;
|
|
m_normalColor = QColor(52, 152, 219); // 蓝色
|
|
m_hoverColor = QColor(41, 128, 185); // 深蓝
|
|
m_currentColor = m_normalColor;
|
|
}
|
|
|
|
ItemExtend::~ItemExtend()
|
|
{
|
|
}
|
|
|
|
void ItemExtend::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
|
{
|
|
painter->save();
|
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
// 绘制圆形背景
|
|
QBrush bgBrush(m_currentColor);
|
|
painter->setBrush(bgBrush);
|
|
painter->setPen(Qt::NoPen);
|
|
painter->drawEllipse(boundingRect());
|
|
|
|
// 绘制白色"+"号
|
|
QPen pen(Qt::white, m_penWidth);
|
|
pen.setCapStyle(Qt::RoundCap);
|
|
pen.setJoinStyle(Qt::MiterJoin);
|
|
painter->setPen(pen);
|
|
|
|
QRectF rect = boundingRect().adjusted(4, 4, -4, -4);
|
|
qreal centerX = rect.center().x();
|
|
qreal centerY = rect.center().y();
|
|
qreal lineLength = rect.width() * 0.4;
|
|
|
|
// 横线
|
|
painter->drawLine(QLineF(centerX - lineLength/2, centerY,
|
|
centerX + lineLength/2, centerY));
|
|
// 竖线
|
|
painter->drawLine(QLineF(centerX, centerY - lineLength/2,
|
|
centerX, centerY + lineLength/2));
|
|
|
|
painter->restore();
|
|
}
|