[英]How can I know if a QGraphicsItem created in a separate class has moved on a scene (changed it's position)?
我已經在新類中創建了GraphicsItem
,並將其發送到另一個文件中的GraphicsView
。 我將標志ItemIsMovable
設置為true,並且能夠移動該項目。
我怎么知道用戶將其移動到了哪里? 而且,我該如何手動設置位置?
[基本上,如果用戶將其移動到足夠接近的正確位置,我想自動將其移動到正確的位置,則我有一個項目]
為了處理鼠標事件,我使用了以下功能:
void Detector::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Pressed = true;
update();
QGraphicsItem::mousePressEvent(event);
}
void Detector::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Pressed = false;
Moving = false;
update();
QGraphicsItem::mouseReleaseEvent(event);
}
void Detector::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
Moving = true;
update();
QGraphicsItem::mouseMoveEvent(event);
}
我目前的想法是也使用繪畫算法,並創建一個if語句,類似於下面顯示的Pressed語句(按下時會更改項目的顏色)。
void Detector::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QBrush darkBrown(QColor(83,71,65));
QBrush clickedBrown(QColor(122,83,66));
//fillBrush.setColor(darkBrown);
QPolygon DetectorPolygon;
DetectorPolygon << QPoint(0,0);
DetectorPolygon << QPoint(15,10);
DetectorPolygon << QPoint(50,10);
DetectorPolygon << QPoint(50,20);
DetectorPolygon << QPoint(15,20);
DetectorPolygon << QPoint(0,30);
QPen borderPen;
borderPen.setWidth(2);
borderPen.setColor(QColor(152,133,117));
painter->translate(780,425);
if (Pressed==true)
{
painter->setBrush(clickedBrown);
}
else
{
painter->setBrush(darkBrown);
}
if (Moving==true)
{
}
else
{
}
painter->setPen(borderPen);
painter->drawPolygon(DetectorPolygon);
}
本質上:如何獲取QGraphicsItem
的坐標以及如何更改它們?
您可以隨時調用QPointF pos() const;
獲得項目和void setPos(const QPointF &pos);
改變他們。 但是應該明確是否只檢查文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.