#include "graphicsItem/electricBayItem.h" #include #include #include ElectricBayItem::ElectricBayItem(const QRectF &rect,QGraphicsItem *parent) : GraphicsNonStandardItem(parent) { m_showRect = rect; m_dWidth = rect.width(); m_dHeight = rect.height(); m_font.setPointSize(12); } ElectricBayItem::~ElectricBayItem() { } void ElectricBayItem::setText(const QString& text) { prepareGeometryChange(); // 通知框架几何变化 m_text = text; updateTextShape(); } QPainterPath ElectricBayItem::shape() { QPainterPath path; path.addRect(_recLabel); return path; } void ElectricBayItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { painter->setPen(m_pen); painter->drawText(_recLabel,m_text); m_pen.setStyle(Qt::DashLine); if (option->state & QStyle::State_Selected) //是选中状态,绘制选中框 { painter->drawRect(m_showRect); } } void ElectricBayItem::updateTextShape() { QFontMetricsF metrics(m_font); QRectF recText = metrics.boundingRect(m_text); //_recLabel = recText.translated(g_offsetX,g_offsetY); _recLabel = recText; shape(); }