Qt 为什么setPos()不在场景中移动QGraphicsSitems?

Qt 为什么setPos()不在场景中移动QGraphicsSitems?,qt,layout,positioning,Qt,Layout,Positioning,在下面的代码中,我有三个QGraphicsSite,它们由QGraphicsLinearLayout布局,QGraphicsLinearLayout设置为QGraphicsWidget的布局 那么,QGraphicsWidget提供了什么我在原始代码中遗漏的功能,以便在容器项上使用setPos可以正确地移动MyShape项的位置?如果不添加MyShape项,但例如在布局中添加三个QGraphicsWidget,是否也会发生这种情况?我正在试图弄清楚MyShape::setGeometry中的se

在下面的代码中,我有三个QGraphicsSite,它们由QGraphicsLinearLayout布局,QGraphicsLinearLayout设置为QGraphicsWidget的布局


那么,QGraphicsWidget提供了什么我在原始代码中遗漏的功能,以便在容器项上使用setPos可以正确地移动MyShape项的位置?

如果不添加MyShape项,但例如在布局中添加三个QGraphicsWidget,是否也会发生这种情况?我正在试图弄清楚MyShape::setGeometry中的setPos调用是否会破坏任何东西。@DerManu:问题是,目前没有实现QGraphicsLayoutItem的QGraphicsWidgets。所以我不知道如何在不实现类的情况下进行测试,如果我这样做了,我就回到了原点;QGraphicsWidget不实现QGraphicsLayoutItem。因此,我尝试将继承从public QGraphicsRectItem、public QGraphicsLayoutItem更改为public QGraphicsWidget,覆盖paint和boundingRect方法,瞧,现在可以了!我猜QGraphicsWidget实现了一些我已经忘记的方法。但是哪个呢?我很想知道。
#include <QApplication>
#include <QBrush>
#include <QDebug>
#include <QGraphicsItem>
#include <QGraphicsLayoutItem>
#include <QGraphicsLinearLayout>
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsWidget>
#include <QPen>

class MyShape : public QGraphicsRectItem, public QGraphicsLayoutItem {
  public:
    MyShape(void) {
        setPen(QPen(QBrush(Qt::black), 1));
        setBrush(QBrush(Qt::green));
        setRect(0, 0, 20, 20);
    }

    virtual QSizeF sizeHint(Qt::SizeHint which,
                            const QSizeF& constraint = QSizeF()) const {
        Q_UNUSED(which);
        Q_UNUSED(constraint);
        return boundingRect().size();
    }

    virtual void setGeometry(const QRectF& rect) {
        setPos(rect.topLeft());
    }
};

int main(int argc, char** argv) {
    QApplication app(argc, argv);

    QGraphicsScene scene;
    MyShape* shape1 = new MyShape;
    MyShape* shape2 = new MyShape;
    MyShape* shape3 = new MyShape;
    scene.addItem(shape1);
    scene.addItem(shape2);
    scene.addItem(shape3);

    QGraphicsLinearLayout* layout = new QGraphicsLinearLayout;
    layout->addItem(shape1);
    layout->addItem(shape2);
    layout->addItem(shape3);

    QGraphicsWidget* container = new QGraphicsWidget;
    container->setLayout(layout);
    scene.addItem(container);

    container->setPos(300, 300); // This doesn't appear to have any affect

    // Item for indicating origin
    QGraphicsRectItem* tmp = scene.addRect(0, 0, 2, 2, QPen(),
                                           QBrush(Qt::green));
    tmp->setPos(0, 0);

    qDebug() << tmp->scenePos();
    qDebug() << container->scenePos();


    QGraphicsView view;
    view.setScene(&scene);
    view.centerOn(0, 0);
    view.show();

    return app.exec();
}
#include <QApplication>
#include <QBrush>
#include <QDebug>
#include <QGraphicsItem>
#include <QGraphicsLayoutItem>
#include <QGraphicsLinearLayout>
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsWidget>
#include <QPen>

class MyShape : public QGraphicsWidget {
  public:
    MyShape(void) {
        setMinimumSize(20, 20);
        setMaximumSize(20, 20);
        setPreferredSize(20, 20);
    }

QRectF boundingRect(void) const {
    QRectF box(0, 0, 22, 22);
    box.translate(box.center() * -1);
    return box;
}

void paint(QPainter* painter,
                 const QStyleOptionGraphicsItem* option,
                 QWidget* widget) {
    Q_UNUSED(option);
    Q_UNUSED(widget);

    // Set box border appearance
    painter->setPen(QPen(Qt::black, 1));

    // Set box background appearance
    painter->setBrush(QBrush(Qt::green));

    QRectF box(0, 0, 20, 20);
    box.translate(box.center() * -1);
    painter->drawRect(box);
}

};

int main(int argc, char** argv) {
    QApplication app(argc, argv);

    QGraphicsScene scene;
    MyShape* shape1 = new MyShape;
    MyShape* shape2 = new MyShape;
    MyShape* shape3 = new MyShape;
    scene.addItem(shape1);
    scene.addItem(shape2);
    scene.addItem(shape3);

    QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(Qt::Vertical);
    layout->addItem(shape1);
    layout->addItem(shape2);
    layout->addItem(shape3);

    QGraphicsWidget* container = new QGraphicsWidget;
    container->setLayout(layout);
    scene.addItem(container);

    container->setPos(200, 200); // This doesn't appear to have any affect
    scene.setSceneRect(-300, -300, 600, 600);

    // Item for indicating origin
    QGraphicsRectItem* tmp = scene.addRect(0, 0, 2, 2, QPen(),
                                           QBrush(Qt::green));
    tmp->setPos(0, 0);

    qDebug() << tmp->scenePos();
    qDebug() << container->scenePos();


    QGraphicsView view;
    view.setScene(&scene);
    view.centerOn(0, 0);
    view.show();

    return app.exec();
}