【问题标题】:Qt: Rotate QGraphicsItem's children around themselves instead of parentQt:围绕自己而不是父母旋转QGraphicsItem的孩子
【发布时间】:2015-08-17 22:30:55
【问题描述】:

我是 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();
}

【问题讨论】:

  • 您是如何移动这些项目的?您是否使用setPos 设置了他们相对于父母的位置?
  • 我尝试了 setPos 和 moveBy... 我还尝试将它们与 setTransformOriginPoint 结合起来移动旋转中心。旋转中心有效地移动了,但 setRotation 函数仍然使用父级的中心。
  • 你能发布一个小的、独立的(单个main.cpp文件)测试用例来重现它吗?在场景中设置并显示视图,向场景中添加两个元素(父节点和子节点),并展示如何移动子节点并应用旋转。

标签: c++ qt qgraphicsitem


【解决方案1】:

我找到了这背后的原因:绝对转换并没有像我想象的那样真正从父母传递给孩子。事实上,当你旋转父级时,只有子级使用的坐标基础被旋转。 简单地说:如果你想在item之间同步旋转,使用父子关系不是正确的方法!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2017-10-13
    • 2023-03-07
    • 2021-08-17
    • 2015-05-05
    • 2012-09-29
    • 1970-01-01
    相关资源
    最近更新 更多