如何围绕选择中心旋转选定的 QGraphicsItems? [英] How to rotate selected QGraphicsItems around the selection center?

查看:72
本文介绍了如何围绕选择中心旋转选定的 QGraphicsItems?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现所选项目的轮换.

I would like to implement rotation of selected items.

我注意到有 2 个函数 - rotate() 执行立即操作但不保存项目的旋转 - 和 setRotation() 存储旋转()(尽管它需要重新绘制导致事件).

I have noticed that there are 2 functions - rotate() which performs an immediate action but does not save the rotation on the item - and setRotation(), which stores rotation() (though it requires a repaint causing event).

如果我选择单个项目并旋转它,我可以设置

If I select a single item and rotate it, I can set

selectedItem->setRotation(selectedItem->rotation() + deg);

这允许项目存储其旋转,当我需要将其复制到另一个场景时,项目保留其旋转信息.所以这就是我需要的.

This allows the item to store its rotation, and when I need to copy it to another scene, the item retains its rotation information. So it is what I need.

虽然尝试旋转多个选定的项目,

Trying to rotate multiple selected items though,

foreach(QGraphicsItem *selectedItem, scene()->selectedItems())
{
    if(selectedItem->flags() & QGraphicsItem::ItemIsMovable)
    {
        selectedItem->setRotation(selectedItem->rotation() + deg);
    }
}
viewport()->update();
foreach(QGraphicsItem *selectedItem, scene()->selectedItems())
{
    qDebug("%f", selectedItem->rotation());
}

这会导致项目围绕自己的中心旋转.要求项目围绕选择中心旋转.其他一切都有效 - 项目将有一个可以稍后使用的轮换.(由 qDebug 显示)

This causes items to rotate around their own center. It is required that items rotate around the center of selection. Everything else works though - items will have a rotation that can be used later. (shown by qDebug)

要让项目围绕选择中心旋转:

To get items to rotate around the selection center:

我尝试过分组、旋转、取消分组.

I have tried to group, rotate, ungroup.

QGraphicsItemGroup* g = scene()->createItemGroup(scene()->selectedItems());
foreach(QGraphicsItem *selectedItem, scene()->selectedItems())
{
    if(!(selectedItem->flags() & QGraphicsItem::ItemIsMovable))
        g->removeFromGroup(selectedItem);
}
g->setRotation(g->rotation() + deg);
scene()->destroyItemGroup(g);
viewport()->update();
foreach(QGraphicsItem *selectedItem, scene()->selectedItems())
{
    qDebug("%f", selectedItem->rotation());
}

这似乎正是我想要的 - 显然.
但是将项目复制到另一个场景,项目没有旋转(它有另一个变换吗?看起来不像).
qDebug 不显示任何内容 - 好像项目已从选择中消失,但它们仍显示选择矩形.(我不清除选择)
按下旋转按钮不会重复旋转.我必须取消选择每个项目,然后重新选择以再次旋转.
此外 - 即使我删除了锁定的项目 - 它们仍然会旋转.(为什么??)
所以它不起作用.

This seems to do exactly what I want - apparently.
But copying the items to another scene, the items do not have rotation (does it have another transform ? It doesn't seem like it).
The qDebug doesn't show anything - as if items have disappeared from selection, but they still show selection rectangle. (I don't clear selection)
Pressing on rotate button doesn't repeat rotation. I must unselect each item, then reselect to get rotation again.
Also - even though I remove locked items - they still rotate. (why ??)
So it just doesn't work.

创建转换而不是旋转...我需要将转换中心设置为选择中心.尝试

Creating a transformation instead of rotation... I would need to set the center of transformation to the center of selection. Trying

QPointF origin = scene()->selectedItems().???
QPointF origin = scene()->selectionArea().???
QPointF origin = scene()->selectionArea().boundingRect().center();  // shows (0, 0)

如果我知道中心在哪里,我可以尝试:

If I do know where the center is, I can try:

// hacky till i figure out
QPointF origin(0,0);
foreach(QGraphicsItem *selectedItem, scene()->selectedItems())
{
    origin.setX(origin.x() + selectedItem->pos().x());
    origin.setY(origin.y() + selectedItem->pos().y());
}
int selSize = scene()->selectedItems().size();
origin.setX(origin.x()/selSize);
origin.setY(origin.y()/selSize);
qDebug("%f %f", origin.x(), origin.y());  // I don't know really if this is the center

foreach(QGraphicsItem *selectedItem, scene()->selectedItems())
{
    if(selectedItem->flags() & QGraphicsItem::ItemIsMovable)
    {
        QPointF origin1 = selectedItem->mapFromScene(origin);

        selectedItem->setTransform(selectedItem->transform().
                                   translate(origin1.x(), origin1.y()).
                                   rotate(deg).
                                   translate(-origin1.x(), -origin1.y()));
    }
}

重大改进......项目作为一个组旋转......但由于某种原因在取消选择并尝试旋转单个项目后,它保留了以前的旋转中心......

Big improvement .. items rotate as a group... but for some reason after unselecting, and trying to rotate an individual item, it retains previous center of rotation...

我该如何进行这种轮换?

How can I do this rotation ?

推荐答案

您需要单独旋转和移动每个项目,以便以后可以独立地将它们移动到另一个场景.您可以手动计算新的旋转和位置:

You need to rotate and move each item individually, so that they can be later moved to another scene independently. You can manually calculate new rotations and positions:

void Form2::on_rotate_clicked() {
  QRectF rect;
  foreach(QGraphicsItem* item, scene->selectedItems()) {
    rect |= item->mapToScene(item->boundingRect()).boundingRect();
  }
  QPointF center = rect.center();
  qreal angle = 10;
  QTransform t;
  t.translate(center.x(), center.y());
  t.rotate(angle);
  t.translate(-center.x(), -center.y());
  foreach(QGraphicsItem* item, scene->selectedItems()) {
    item->setPos(t.map(item->pos()));
    item->setRotation(item->rotation() + angle);
  }
}

这篇关于如何围绕选择中心旋转选定的 QGraphicsItems?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆