1

在下面的代码中,我有 3个由 布局QGraphicsItem的 s QGraphicsLinearLayout,它们被设置为 a 的布局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(),但它似乎不起作用(QGraphicsItems 仍然在同一个地方)。但是,自从滚动条发生变化后,似乎发生了一些事情。看来,这个QGraphicsWidget动作,没有带QGraphicsItems 随之而来。

为什么?

编辑:

根据DerManuMyShape的建议,我更改了to的继承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

4

0 回答 0