[Graphics View] QGraphicsItem绕中心旋转

以QGraphicsRectItem为例,需要让显示的矩形绕矩形中心旋转,需要用到setTransformOriginPoint以及setRotation函数。
代码如下:

#include "widget.h"
#include "ui_widget.h"
#include <QGraphicsScene>
#include <QGraphicsRectItem>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);


    QGraphicsScene *scene = new QGraphicsScene;
    //-2是为了不显示scrollbar
    scene->setSceneRect(0, 0, ui->view->width()-2, ui->view->height()-2);
    ui->view->setScene(scene);
    for(int i=0; i<4; i++)
    {
        QGraphicsRectItem *item = new QGraphicsRectItem;
        item->setPen(QPen(QColor("red")));
        item->setPos(QPointF(scene->width()/2, scene->height()/2));
        item->setRect(0, 0, scene->width()/4, scene->width()/4);
        scene->addItem(item);
    }

    int i=0;
    for(auto &item : scene->items())
    {
        auto rect = item->boundingRect();
        item->setTransformOriginPoint(QPointF(rect.x(), rect.y())+QPointF(rect.width()/2, rect.height()/2));
        item->setRotation(i++*15);
    }

}

Widget::~Widget()
{
    delete ui;
}

效果图:


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,466评论 2 59
  • 【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...
    Rtia阅读 11,368评论 1 38
  • 1 CALayer IOS SDK详解之CALayer(一) http://doc.okbase.net/Hell...
    Kevin_Junbaozi阅读 10,569评论 3 23
  • 【Android 自定义View之绘图】 基础图形的绘制 一、Paint与Canvas 绘图需要两个工具,笔和纸。...
    maiduoduo阅读 8,484评论 0 3
  • 1 摘要 上一章主要介绍了图像卷积操作,此外我们还可以对图像做很多有趣的操作,如使用一个较小的窗口在图像上扫描以完...
    RichardJieChen阅读 5,806评论 0 8