QGraphicsItem::pos returns 0 在布局中

QGraphicsItem::pos returns 0 when in layout

我的 widget.cpp 中有以下 Qt 代码片段:

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
    m_Scene = new QGraphicsScene(this);
    m_Scene->setSceneRect(0, 0, 1024, 768);

    GraphicsTextItem* m_1 = new GraphicsTextItem(nullptr, QString("l_1"));

    GraphicsTextItem* m_2 = new GraphicsTextItem(nullptr, QString("l_2"));


    QGraphicsLinearLayout* layout = new QGraphicsLinearLayout;
    layout->addItem(m_1);
    layout->addItem(m_2);

    QGraphicsWidget* list = new QGraphicsWidget;
    list->setLayout(layout);
    m_Scene->addItem(list);

    qDebug() << m_2->x() << " " << m_2->y(); // Prints 0,0 Why?

    QGraphicsView* view = new QGraphicsView(this);
    view->setScene(m_Scene);
}

GraphicsTextItem 是 QGraphicsWidget 的派生 class:

class GraphicsTextItem : public QGraphicsWidget
{
private:
    QString m_Name;
public:
    GraphicsTextItem(QGraphicsItem * parent = nullptr, const QString& name = QString());
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
    {
        QFont font("Times", 12);
        painter->setFont(font);
        painter->drawText(0, 0, m_Name);
    }
};

我也把我的short main放在main.cpp:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

我的问题是为什么 qDebug 行打印 0,0 因为小部件的位置肯定是非零的。如果我不将小部件放在布局中并调用 setPos() qDebug 打印之前设置的正确值。

所有QGraphicsItem的初始位置是(0, 0),包括m_2,m_2会在应用QGraphicsLinearLayout时改变其位置,这将是同步任务后的瞬间完成并且事件循环开始工作,这可以使用 QTimer::singleShot(0, ...):

观察
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    m_Scene = new QGraphicsScene(this);
    QGraphicsView* view = new QGraphicsView(this);
    view->setScene(m_Scene);

    m_Scene->setSceneRect(0, 0, 1024, 768);

    GraphicsTextItem *m_1 = new GraphicsTextItem(nullptr, QString("l_1"));

    GraphicsTextItem *m_2 = new GraphicsTextItem(nullptr, QString("l_2"));


    QGraphicsLinearLayout* layout = new QGraphicsLinearLayout;
    layout->addItem(m_1);
    layout->addItem(m_2);

    QGraphicsWidget* list = new QGraphicsWidget;
    list->setLayout(layout);
    m_Scene->addItem(list);

    qDebug() << "synchronous" << m_2->pos() << m_2->mapToScene(QPointF{});

    QTimer::singleShot(0, m_2, [m_2](){
        qDebug() << "asynchronous"<< m_2->pos() << m_2->mapToScene(QPointF{});
    });
}

输出:

synchronous QPointF(0,0) QPointF(0,0)
asynchronous QPointF(62,6) QPointF(62,6)