如何从点击的 QGraphicsItem 获取信息?
How to get info from clicked QGraphicsItem?
这个想法是用户点击一个形状,形状的信息显示在 table 上。如果用户 select 形状(将鼠标拖到形状上),这很有效。我正在尝试修改此代码以执行该操作,但并不幸运。这就是我为 select 模式所做的:
我有一个电话:
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;
}
}
问题是:
//we get index of selected shape
QGraphicsItem *selected = this->selectedItems().first();
单击未 select 形状时如何执行此操作?
我尝试修改mousePressEvent中形状的子类:
if (event->button() == Qt::MouseButton::LeftButton) {
this->setSelected(true);
}
任何人都可以帮助找到解决方案吗?
谢谢。
QList<QGraphicsItem *> QGraphicsView::items(const QPoint &pos) const
Returns a list of all the items at the position pos in the view. The
items are listed in descending stacking order (i.e., the first item in
the list is the uppermost item, and the last item is the lowermost
item). pos is in viewport coordinates.
重载使用示例 QGraphicsView::mousePressEvent()
:
void CustomView::mousePressEvent(QMouseEvent *event) {
qDebug() << "There are" << items(event->pos()).size()
<< "items at position" << mapToScene(event->pos());
}
这个想法是用户点击一个形状,形状的信息显示在 table 上。如果用户 select 形状(将鼠标拖到形状上),这很有效。我正在尝试修改此代码以执行该操作,但并不幸运。这就是我为 select 模式所做的:
我有一个电话:
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;
}
}
问题是:
//we get index of selected shape
QGraphicsItem *selected = this->selectedItems().first();
单击未 select 形状时如何执行此操作?
我尝试修改mousePressEvent中形状的子类:
if (event->button() == Qt::MouseButton::LeftButton) {
this->setSelected(true);
}
任何人都可以帮助找到解决方案吗?
谢谢。
QList<QGraphicsItem *> QGraphicsView::items(const QPoint &pos) const
Returns a list of all the items at the position pos in the view. The items are listed in descending stacking order (i.e., the first item in the list is the uppermost item, and the last item is the lowermost item). pos is in viewport coordinates.
重载使用示例 QGraphicsView::mousePressEvent()
:
void CustomView::mousePressEvent(QMouseEvent *event) {
qDebug() << "There are" << items(event->pos()).size()
<< "items at position" << mapToScene(event->pos());
}