如何知道在单独类中创建的 QGraphicsItem 是否已在场景中移动(更改其位置)?
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);
}
我目前的想法是也使用绘制算法,并创建一个类似于下面为Pressed显示的if语句(按下时会改变项目的颜色)。
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);
来更改它们。但是,如果您只查看文档,应该很清楚。
相关文章:
- 将光标单独移动到当前位置
- 移动 std::bitset<N> 是否超过 N 个位置未定义的行为?
- 为什么当我改变光源的位置时,光源不移动?
- 在旋转 cv::Mat 而不裁剪后,将 cv::RotatedRect 移动到相同的位置
- 快速从文件中读取下一个字符串,而无需移动文件的位置
- 在鼠标位置移动精灵
- 将过滤器中的所有.cpp和.h移动到另一个项目并更正其文件夹位置
- 在链接列表前n位置中移动某个节点
- QToolButton 的位置在调整大小或移动父小部件时不会改变
- C 指向对象,然后移动其内存位置
- 是STD :: Vector,由于其分配运算符移动其成员的位置,因此是一种非常规类型
- 如何在不删除任何字符的情况下将字符从字符串中的某个位置移动到其最前面
- 地图移动位置与左值
- 在 3 点之间移动模型会导致模型偏离位置
- QT4 QListView保存图标位置下一次由手动移动
- 将指针移动到其他位置后删除分配的内存
- SFML中移动位置的居中视图
- Qt QDialog每次关闭并再次显示时都会稍微移动位置
- 为什么我可以移动位置指示灯(SetFilePointerEx)在文件只有512倍
- 处理嘈杂的移动位置