首页 > 解决方案 > Qt - 如何正确找到孩子

问题描述

我正在尝试找到一个添加到屏幕的小部件并对其进行一些操作,但子列表每次都是空的。我究竟做错了什么?这是代码(要运行它,创建一个新的干净项目并替换 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;
}

标签: c++qt

解决方案


推荐阅读