1

我想解析QGraphicsGridLayout,但得到这个错误。问题是我能够添加QGraphicsProxyWidget为项目但无法检索它。我想知道如何QGraphicsLayoutItem*QGraphicsProxyWidget*这行mainwindow.cpp代码中将 a 转换为auto *currentWidget = static_cast<QGraphicsProxyWidget*>(layout->itemAt(row, col));。使用它时static_cast返回一个NULL指针,请帮忙。

主窗口.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGraphicsGridLayout>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPushButton>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    QVector<QString> node_vector = {"abc", "def", "ghi", "jkl"};

    QGraphicsGridLayout *layout;
    QGraphicsView *view;
    QGraphicsScene *scene;

public slots:
    void builGridlayout ();
    void updateGridlaout ();

private:
    Ui::MainWindow *ui;
    QPushButton *updateGridlayout;
    QWidget *menu;
};
#endif // MAINWINDOW_H

主窗口.cpp:

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

#include <QGridLayout>
#include <QLabel>
#include <QGraphicsWidget>
#include <QGraphicsProxyWidget>
#include <QDebug>

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

    /* building the gridLayout when creating the QMainWindow */
    builGridlayout();

    menu = new QWidget;
    QHBoxLayout *menuLayout = new QHBoxLayout(menu);
    menu->setLayout(menuLayout);

    /* creating the button to update GridLayout */
    updateGridlayout = new QPushButton("update gridlayout");
    menuLayout->addWidget(updateGridlayout);
    connect(updateGridlayout, &QPushButton::released, this, &MainWindow::updateGridlaout);

    menuLayout->addStretch();
    this->setMenuWidget(menu);
}

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

/* creating a GraphicsGridLayout with QGraphicsProxyWidget */
void MainWindow::builGridlayout()
{
    layout = new QGraphicsGridLayout;
    scene = new QGraphicsScene(this);

    QGraphicsWidget *window = new QGraphicsWidget();

    int numCols = 4,row = 0,col = 0;
    for(int index = 0; index < node_vector.size(); index++){

        QString node = node_vector[index];

        QLabel *label = new QLabel;
        label->setText(node);
        label->setMargin(5);

        /* converting the QLabel into QGraphicsProxyWidget and
         * adding it the QGraphicsGridLayout*/
        QGraphicsProxyWidget *stringLabel = scene->addWidget(label);
        layout->addItem(stringLabel,row+1, col+1);

        col++;
        if(col == numCols) row++;
        col = col % numCols;
    }

    window->setLayout(layout);
    scene->addItem(window);

    view = new QGraphicsView(scene, this);
    setCentralWidget(view);
    view->setGeometry(0, 0, this->maximumWidth(), this->maximumHeight());
    view->show();
}

/* method to update the QGraphicsProxyWidget present in the QGrpahicsGridLayout */
void MainWindow::updateGridlaout()
{
    int itemCount = layout->count();

    int numCols = 4;
    int row = 0, col = 0;
    for (int i = 0; i < itemCount; ++i)
    {
        auto *currentWidget = static_cast<QGraphicsProxyWidget*>(layout->itemAt(row, col));

        if (currentWidget) qDebug() << "successful";
        else qDebug() << "getting nullptr";

        col++;
        if (col == numCols) row++;
        col = col % numCols;
    }
}
4

1 回答 1

0

原因

QGraphicsProxyWidget是 a QGraphicsWidget它又是QGraphicsObject QGraphicsLayoutItem

QGraphicsGridLayout::itemAt返回QGraphicsLayoutItem,因此通过这样写:

QGraphicsProxyWidget *currentWidget = layout->itemAt(row, col);

编译器仅识别QGraphicsLayoutItem此多重继承的 - 部分并给您一个错误。

解决方案

明确layout->itemAt(row, col)地转换QGraphicsProxyWidget成这样:

auto *currentWidget = static_cast<QGraphicsProxyWidget *>(layout->itemAt(row, col));

注意:你必须确定,这个位置的项目确实是QGraphicsProxyWidget. 如果你有某种QGraphicsLayout替代,你的程序将会崩溃。

于 2021-11-22T05:44:32.553 回答