我想解析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;
}
}