我有 QGraphicsScene
,上面显示了一些自定义 QGraphicsItems
。这些项目在 MeasurePoint
类中描述,该类继承自 QGraphicsItem
。它们也存储在 QList
中,因此每个项目都有它的索引。它们像这样添加到场景中:
void MeasureSpline::addNode(qreal xPos, qreal yPos, QGraphicsScene *scene)
{
MeasurePoint *point = new MeasurePoint(xPos, yPos, points.size());
points.append(point);
point->setPoint(scene);
}
points
Đúng:
QList points;
mỗi MeasurePoint
的构造如下:
MeasurePoint::MeasurePoint(qreal a, qreal b, int c)
{
xPos = a;
yPos = b;
index = c;
movable = false;
selected = false;
}
VàsetPoint()
Đúng:
void MeasurePoint::setPoint(QGraphicsScene *scene)
{
scene->addItem(this);
}
我有一个设置项目可移动的方法。如果我使用这种方法,项目就会变得可移动,我对结果很满意。但我目前的目标是知道目前正在移动哪些项目。可以吗?如何?感谢您的帮助。
首先,让 QGraphicsItem 像这样对位置变化使用react:
item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable |
QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemSendsScenePositionChanges);
然后您可以重新实现 Change-Event 并从那里发出信号:
QVariant Item::itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange && scene() || change == ItemScenePositionHasChanged) // either during mouseMoveEvent or when Dropped again
{
emit itemMoved(); // connect to other SLOT and cast QObject::sender() or something here....
}
return QGraphicsItem::itemChange(change, value);
}
biên tập:
接收方法未经测试的代码:
void MyClass::onItemMoved()
{
MesurePoint* item = dynamic_cast(QObject::sender());
if (item != NULL)
{
int index = points.IndexOf(item);
}
}
Tôi là một lập trình viên xuất sắc, rất giỏi!