我试图找到添加到屏幕上的小部件并对其进行一些操作,但是子级列表每次都是空的。我究竟做错了什么? 这是代码(运行它,创建一个新的干净项目并替换MainWindow.cpp中的文本):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QLabel>
#include <QLayout>
#include <QList>
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QLabel* wid = new QLabel("hello");
ui->centralWidget->layout()->addWidget(wid);
QList<QLabel*> list = ui->centralWidget->findChildren<QLabel*>();
qDebug() << list.isEmpty();
}
MainWindow::~MainWindow()
{
delete ui;
}