Qt:将QGraphicsItem的孩子转移到自己身边而不是父母身边

时间:2015-08-17 22:30:55

标签: c++ qt qgraphicsitem

我是Qt的初学者,我正在寻找基于小部件的应用程序的一些帮助......我有一个QGraphicsItem,其中3个相同的孩子放置在不同的偏移处。我想要实现的是同步父和子之间的转换。目前,我使用setRotation函数来实现旋转,但它会围绕父级旋转中心旋转子项。我找不到如何围绕自己的中心旋转每个孩子。有人可以帮忙吗?

编辑:示例:

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>

int main(int argc, char *argv[])
{
    //setting up the window
    QApplication a(argc, argv);
    QGraphicsScene* scene = new QGraphicsScene;
    scene->setSceneRect(0,0,800,600);
    QGraphicsView* view = new QGraphicsView(scene);
    view->setFixedSize(800,600);

    //adding 2 squares (parent and child
    QGraphicsRectItem * parentItem= new QGraphicsRectItem(0,0,20,20);
    scene->addItem(parentItem);
    QGraphicsRectItem * childItem =new QGraphicsRectItem(0,0,20,20,parentItem);
    //moving child 100px to the right
    childItem->setPos(100,0);
    //childItem->setTransformOriginPoint(100,0);//uncomment to move child's centre of rotation

    //rotating 15 deg
    parentItem->setRotation(15);

    view->show();
    return a.exec();
}

1 个答案:

答案 0 :(得分:0)

我发现了背后的原因:绝对的转变并没有像我想象的那样从父母传给孩子。实际上,当您旋转父级时,只有子级使用的坐标基础会旋转。 简单地说:如果你想同步项目之间的轮换,使用父/子关系不是正确的方法!