You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.3 KiB
C++
72 lines
1.3 KiB
C++
#include "MyQGraphicsRectItem.h"
|
|
|
|
void MyQGraphicsRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
{
|
|
if (event->button() == Qt::LeftButton)
|
|
{
|
|
if (Base() != true && Mark() != true) {
|
|
|
|
m_isSelect = !m_isSelect;
|
|
|
|
if (m_isSelect)
|
|
{
|
|
m_isShowTextItem = true;
|
|
setBrush(CUR_SELECT_DIE);
|
|
}
|
|
else
|
|
{
|
|
if (m_isAlreadlyMarked)
|
|
{
|
|
setBrush(DIE_MARK_END);
|
|
}
|
|
else
|
|
{
|
|
setBrush(Qt::NoBrush);
|
|
}
|
|
}
|
|
|
|
ShowTextItemFunc();
|
|
|
|
emit signal_select(m_realDieXCoord, m_realDieYCoord);
|
|
}
|
|
}
|
|
}
|
|
|
|
void MyQGraphicsRectItem::ShowTextItemFunc()
|
|
{
|
|
if (m_textItem == nullptr)
|
|
{
|
|
m_textItem = new QGraphicsTextItem(this);
|
|
}
|
|
if (m_isShowTextItem && m_isSelect)
|
|
{
|
|
m_textItem->setPlainText(m_serialNum);
|
|
}
|
|
else
|
|
{
|
|
m_textItem->setPlainText("");
|
|
}
|
|
if (!m_isSelect)
|
|
{
|
|
return;
|
|
}
|
|
// 2. 设置默认字体和颜色
|
|
QFont font("Microsoft YaHei", 1);
|
|
m_textItem->setFont(font);
|
|
m_textItem->setDefaultTextColor(Qt::black);
|
|
|
|
if (!m_textItem) return;
|
|
|
|
// 获取文本包围盒
|
|
QRectF textRect = m_textItem->boundingRect();
|
|
// 获取当前矩形包围盒
|
|
QRectF itemRect = this->rect();
|
|
|
|
// 计算居中位置
|
|
qreal xPos = (itemRect.width() - textRect.width()) / 2.0;
|
|
qreal yPos = (itemRect.height() - textRect.height()) / 2.0;
|
|
|
|
m_textItem->setPos(xPos, yPos);
|
|
m_textItem->setZValue(1000);
|
|
}
|