c++ - 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;
}
解决方案
推荐阅读
- javascript - React 原生 props 声明混淆
- oracle - 更改 currval 的 Oracle 序列为什么不直接删除它并重新创建?
- r - 对几个文本文件进行计算并从中制作数据框 R
- html - 在 HTML 中为 R Shiny 着色和突出显示文本
- r - 什么时候使用 table_list?
- php - 如何从另一个表中获取数据?这样我就可以提出条件并建立关系(Laravel + Vue)
- vue.js - Animating a Vue component using mouseover and mouseout
- python - 加快 pandas 成对创建
- python - 用 Pandas 绘制两列或多列的切片(时间索引)
- c# - 使用子数组连接两个集合