我一直在尝试使用QGraphicsItemGroup来获取一组QGraphicsItem * s的边界矩形。在我看来,当我将所有项目插入到组中时,正确确定了边界矩形。但是,如果我随后在组中移动了项目,则边框矩形不会更新为包含我期望的已移动项目。我在文档中找不到关于我所看到的是否正确的行为的指示;我的猜测是我要么误解了QGraphicsItemGroup的工作原理,要么误用了它

我一直在测试的一个示例:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QTransform>
#include <QGraphicsEllipseItem>
#include <QDebug>
#include <QTimer>

#include <cmath>

const double pi = 3.14;

QTransform rotation(double degrees)
{

    double a    = pi/180 * degrees;
    double sina = sin(a);
    double cosa = cos(a);

    return QTransform(cosa, sina, -sina, cosa, 0, 0);
}

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

    scene = new QGraphicsScene(this);
    ui->graphicsView->setScene(scene);
    ui->graphicsView->show();

    //Shouldn't execute until after window.show() and application.exec()
    //are called
    QTimer::singleShot(1, this, &MainWindow::build);
}

void MainWindow::build()
{
    QGraphicsEllipseItem* el1 = scene->addEllipse(-5, -5, 10, 10);
    scene->addLine(0, 0, 0, 10)->setParentItem(el1);

    QGraphicsEllipseItem* el2 = scene->addEllipse(-5, -5, 10, 10);
    scene->addLine(0, 0, 0, 10)->setParentItem(el2);

    QGraphicsEllipseItem* el3 = scene->addEllipse(-5, -5, 10, 10);
    scene->addLine(0, 0, 0, 10)->setParentItem(el3);

    QGraphicsEllipseItem* el4 = scene->addEllipse(-5, -5, 10, 10);
    scene->addLine(0, 0, 0, 10)->setParentItem(el4);

    QGraphicsItemGroup* group = new QGraphicsItemGroup;
    group->addToGroup(el1);
    group->addToGroup(el2);
    group->addToGroup(el3);
    group->addToGroup(el4);

    scene->addItem(group);

    scene->addRect(group->boundingRect());

    QTransform translate2(1, 0, 0, 1, 10, 10);
    QTransform t2 = /*rotation(45) **/ translate2;
    el2->setTransform(t2);

    QTransform translate3(1, 0, 0, 1, 20, 20);
    QTransform t3 = /*rotation(-45) **/ translate3 * t2;
    el3->setTransform(t3);

    QTransform translate4(1, 0, 0, 1, 20, -20);
    QTransform t4 = translate4 * t3;
    el4->setTransform(t4);

    qDebug() << t4.dx() << t4.dy() << atan2(t4.m12(), t4.m22())*180/pi;
    QTransform t4i = t4.inverted();
    qDebug() << t4i.dx() << t4i.dy() << atan2(t4i.m12(), t4i.m22())*180/pi;

    scene->addRect(group->boundingRect());
}

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

显示的最终场景如下所示

实际结果

但是,我期待这样的事情,第一个boundingRect中有一个小盒子,第二个中有一个大盒子

预期结果

我是否误解了QGraphicsItemGroup的工作方式,还是使用不正确?

Qt版本:5.10

操作系统:Ubuntu 16.04

编译器GCC 5.4

最佳答案

QGraphicsItemGroup文档似乎并未提及这一点,但仅在显示 View 和boundingRect之后才重新计算QGraphicsItemGroupQGraphicsScene

之后添加项目

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene scene;
    QGraphicsView mainView(&scene);
    scene.setSceneRect(0, 0, 800, 800);

    QGraphicsEllipseItem* el1 = scene.addEllipse(-5, -5, 10, 10);
    scene.addLine(0, 0, 0, 10)->setParentItem(el1);

    QGraphicsEllipseItem* el2 = scene.addEllipse(-5, -5, 10, 10);
    scene.addLine(0, 0, 0, 10)->setParentItem(el2);

    QGraphicsEllipseItem* el3 = scene.addEllipse(-5, -5, 10, 10);
    scene.addLine(0, 0, 0, 10)->setParentItem(el3);

    QGraphicsEllipseItem* el4 = scene.addEllipse(-5, -5, 10, 10);
    scene.addLine(0, 0, 0, 10)->setParentItem(el4);

    QGraphicsItemGroup* group = new QGraphicsItemGroup;
    scene.addItem(group);


    QTransform translate2(1, 0, 0, 1, 10, 10);
    QTransform t2 = /*rotation(45) **/ translate2;
    el2->setTransform(t2);

    QTransform translate3(1, 0, 0, 1, 20, 20);
    QTransform t3 = /*rotation(-45) **/ translate3 * t2;
    el3->setTransform(t3);

    QTransform translate4(1, 0, 0, 1, 20, -20);
    QTransform t4 = translate4 * t3;
    el4->setTransform(t4);

    mainView.show();

    group->addToGroup(el1);
    group->addToGroup(el2);
    group->addToGroup(el3);
    group->addToGroup(el4);
    scene.addRect(group->boundingRect());
    qDebug() << group->sceneBoundingRect() << endl << group->boundingRect();
    scene.addRect(group->sceneBoundingRect());

    return a.exec();
}

导致组中所有成员的边界区域

c&#43;&#43; - QGraphicsItemGroup::boundingRect()不更新-LMLPHP

关于c++ - QGraphicsItemGroup::boundingRect()不更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49163751/

10-13 06:59