Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt:在QGraphicscene中,选定的项目显示在顶部_C++_Qt_Qgraphicsview_Qgraphicsitem_Qgraphicsscene - Fatal编程技术网

C++ Qt:在QGraphicscene中,选定的项目显示在顶部

C++ Qt:在QGraphicscene中,选定的项目显示在顶部,c++,qt,qgraphicsview,qgraphicsitem,qgraphicsscene,C++,Qt,Qgraphicsview,Qgraphicsitem,Qgraphicsscene,因此,我有一个带有各种项目的qgraphicscene。其中一些可以在场景中采用相同的坐标。当我显示场景时,顶部显示的是最后添加的场景。是否有办法使某个项目始终显示在顶部,或者更好地使其在程序期间重新显示在顶部(而不是删除并再次添加)?。我一直在考虑预先绘制所有内容,然后使用setVisible()仅显示我想要的项目,但由于我想在线添加新内容,这似乎有点问题。您可以使用QGraphicsItem::setZValue(qreal z)设置项目的z值。Z值决定兄弟项目的堆叠顺序。Z值较高的项目将始

因此,我有一个带有各种项目的
qgraphicscene
。其中一些可以在场景中采用相同的坐标。当我显示场景时,顶部显示的是最后添加的场景。是否有办法使某个项目始终显示在顶部,或者更好地使其在程序期间重新显示在顶部(而不是删除并再次添加)?。我一直在考虑预先绘制所有内容,然后使用
setVisible()
仅显示我想要的项目,但由于我想在线添加新内容,这似乎有点问题。

您可以使用
QGraphicsItem::setZValue(qreal z)
设置项目的z值。Z值决定兄弟项目的堆叠顺序。Z值较高的项目将始终绘制在Z值较低的其他项目的顶部。默认Z值为零。因此,您可以将其设置为更高的值以使其位于顶部:

item->setZValue(1);
或将其置于其他项目的底部:

item->setZValue(-1);
检查这个


在做了一些研究之后,我认为@Leslie的答案在某种程度上比@Nejat的答案更正确。但是,如果您有很多项目,并且必须经常进行检查,则效率非常低

在我的案例中,我有一个
QGraphicsTextItem
,它显示了场景中光标的坐标

void QCVN_SceneView::mouseMoveEvent(QMouseEvent *event)
{
  QPointF cursorPoint = mapToScene(event->pos());
//  cout << "Event pos: x=" << event->pos().x() << ", y=" << event->pos().y() << endl;
//  cout << "Event pos (in scene): x=" << cursorPoint.x() << ", y=" << cursorPoint.y() << endl;

  QString coords = QString("%1, %2")
                          .arg(cursorPoint.x())
                          .arg(cursorPoint.y());

  cursorSceneCoords->setPlainText(coords);
  cursorSceneCoords->setPos(cursorPoint);

  QGraphicsView::mouseMoveEvent(event);
}

这个项目总是在其他项目之上。

这个问题有点让人困惑。主题提到了在选择时想要在顶部的项目,而描述谈到了更持久的订购问题(创建等)。我添加这个答案是为了澄清由于这个主题而来到这里的人可能的解决方案:

  • 对于永久订单来说,深度管理是一条必由之路。例如,具有组背景的节点视图,其中背景应始终位于所有其他项目的后面,
    zValue
    可以设置为-100
  • 对于临时订购(如选择),您不想干扰永久订购(应保留用于用户集或特定于应用程序的订购状态需要),可以使用。这将更改同一个
    zDepth
    中项目的顺序。 因此,当选择项目时,您可以运行所有项目并调整顺序,您不必担心在完成后将
    zDepth
    重置回之前的状态(我想您仍然可以恢复,但至少这样可以将关注点从
    zDepth
    中分离出来)。注意,FWIW,我发现
    stackBefore
    sibling
    的排序感觉向后

void主窗口::在操作层上触发
{
如果(场景->选择编辑项().size()>0){
QList collingitems=scene->collingitems(scene->selectedItems().first());
对于(int i=0;istackBefore(场景->selectedItems().first());
}
场景->更新();
}
}

1
可能的最高Z值吗?因为如果不是,则@Leslie的答案将是正确的(尽管性能不好)。@rbaleksandar Z值可能大于
1
。管理Z顺序的逻辑依赖于应用程序,可以通过多种方式完成。
void QCVN_SceneView::mouseMoveEvent(QMouseEvent *event)
{
  QPointF cursorPoint = mapToScene(event->pos());
//  cout << "Event pos: x=" << event->pos().x() << ", y=" << event->pos().y() << endl;
//  cout << "Event pos (in scene): x=" << cursorPoint.x() << ", y=" << cursorPoint.y() << endl;

  QString coords = QString("%1, %2")
                          .arg(cursorPoint.x())
                          .arg(cursorPoint.y());

  cursorSceneCoords->setPlainText(coords);
  cursorSceneCoords->setPos(cursorPoint);

  QGraphicsView::mouseMoveEvent(event);
}
YOUR_GRAPHICS_ITEM.setZValue(std::numeric_limits<qreal>::max());
void MainWindow::on_action_layerTop_triggered()
{
    if(scene->selectedItems().size() > 0){
        QList<QGraphicsItem*> collidingItems = scene->collidingItems(scene->selectedItems().first());
        for(int i=0; i<collidingItems.size(); i++){
            collidingItems.at(i)->stackBefore(scene->selectedItems().first());
        }
        scene->update();
    }
}