QGraphicsEllipseItem不可移动

QGraphicsEllipseItem not movable

本文关键字:可移动 QGraphicsEllipseItem      更新时间:2023-10-16

我正在MAC OS X/QT5.3小部件应用程序中工作,我正试图使QGraphicsEllipseItemQGraphicsItemGroup中移动。我正在设置所有需要的标志,但是椭圆不会移动。

下面是我的代码(省略了不重要的和简化的):

 QGraphicsView * view;
 QGraphicsItemGroup * controlPoints;
 //there are more groups
 void setUp()
 {
    view = new QGraphicsView();
    view->setRenderHint(QPainter::Antialiasing,true);
    QGraphicsScene * scene = new QGraphicsScene(0,0,500,500);
    view.setScene(scene);
    controlPoints = new QGraphicsItemGroup();
    //there are multiple groups and i need them to be in different depths(zvalues)
    controlPoints->setZValue(2);
    scene.addItem(controlPoints);
 }
 void AddPointsToGroup()
 {
    QGraphicsEllipseItem * controlPoint = new QGraphicsEllipseItem(100,100,6.5,6.5);
    controlPoint->setFlag(QGraphicsItem::ItemIsMovable,true);
    controlPoint->setFlag(QGraphicsItem::ItemIsSelectable,true);
    controlPoint->setFlag(QGraphicsItem::ItemIsFocusable,true);
    controlPoint->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
    controlPoints->addToGroup (controlPoint);
 }

现在,这个点已经显示出来了,但是不能移动

正如Qt文档所述:-

QGraphicsItemGroup类提供了一个容器,它将一组项视为单个项。

因此,您不应该期望在组中移动单个项。如果你想移动组作为一个整体,你需要设置组本身的标志。

由于QGraphicsItem和派生类可以是父类,您可以通过创建单个QGraphicsItem来实现所需的效果,您可以将该QGraphicsItem用作所有您想要组合在一起的类的父类。

这将允许移动单独的图形项成员。