Qt 如何从点击的QGraphicsItem获取信息?
其思想是,用户点击一个形状,该形状的信息显示在表格上。如果用户选择该形状(将鼠标拖动到该形状上),则效果良好。我正试图修改此代码以执行该操作,但并不幸运。这就是我在选择模式下所做的: 我打电话到:Qt 如何从点击的QGraphicsItem获取信息?,qt,click,qgraphicsitem,Qt,Click,Qgraphicsitem,其思想是,用户点击一个形状,该形状的信息显示在表格上。如果用户选择该形状(将鼠标拖动到该形状上),则效果良好。我正试图修改此代码以执行该操作,但并不幸运。这就是我在选择模式下所做的: 我打电话到: void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); 释放鼠标后,我将更新数据: //enter the mode for selecting if(theMode == SelectObject){ if (this->i
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
释放鼠标后,我将更新数据:
//enter the mode for selecting
if(theMode == SelectObject){
if (this->items().isDetached()){
//we check if the object is selected
if (this->selectedItems().isEmpty()){
qDebug() << "not selected";
isSelected = false;
return;
}
//we get a list of the shapes
QList<QGraphicsItem*> stackOfShapes = this->items();
//we get index of selected shape
QGraphicsItem *selected = this->selectedItems().first();
int indexOfShape = stackOfShapes.indexOf(selected);
//we see which shape is (For a Rectangle)
switch (selected->type()) {
case 346:
{
updateDataOfRect();
}
break;
}
}
如果单击形状未选中,如何执行此操作
我试图修改mousePressEvent中形状的子类:
if (event->button() == Qt::MouseButton::LeftButton) {
this->setSelected(true);
}
有人能帮忙找到解决办法吗
谢谢
QList<QGraphicsItem *> QGraphicsView::items(const QPoint &pos) const
QList<QGraphicsItem *> QGraphicsView::items(const QPoint &pos) const
void CustomView::mousePressEvent(QMouseEvent *event) {
qDebug() << "There are" << items(event->pos()).size()
<< "items at position" << mapToScene(event->pos());
}