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

时间:2012-04-19 16:18:39

标签: qt layout positioning

在下面的代码中,我有三个由QGraphicsItem布局的QGraphicsLinearLayout,其设置为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();
}

然后,我尝试通过调用QGraphicsWidget移动场景中的setPos(),但它似乎不起作用(QGraphicsItem仍保留在同一位置)。但是,自滚动条更改后,某些似乎发生了。似乎QGraphicsWidget移动了,而没有带QGraphicsItem s。

为什么?

编辑:

根据 DerManu 的建议,我将MyShape的继承更改为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 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();
}

那么QGraphicsWidget提供了什么,我错过了原始代码,以便在容器项上使用setPos正确移动MyShape项的位置?

0 个答案:

没有答案