我想解析QGraphicsGridLayout
,但收到此错误。问题是我能够QGraphicsProxyWidget
添加为项目,但无法检索它。我想知道如何将QGraphicsLayoutItem*
转换为QGraphicsProxyWidget*
这行mainwindow.cpp
代码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;
}
}
原因
QGraphicsProxyWidget
是一个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
,您的程序将崩溃。