【发布时间】: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