方法1:通过对象名称动态查找控件
// 假设控件在UI中已设置对象名称(如通过Qt Designer设置)
QTextEdit* QTextEdit_control = this->findChild<QTextEdit*>("textEdit");
if (QTextEdit_control) {
QString value = QTextEdit_control->toPlainText(); // 获取文本框内容
QMessageBox::information(this, "tip", value);
}
// 查找单个控件(返回第一个匹配项)
QLineEdit* lineEdit = frame->findChild<QLineEdit*>("lineEditObjectName");
// 查找所有同名控件(返回列表)
QList<QPushButton*> buttons = frame->findChildren<QPushButton*>("buttonObjectName");
方法2:通过控件类型查找(模糊匹配)
// 查找所有QCheckBox(无论名称)
QList<QCheckBox*> checkBoxes = this->findChildren<QCheckBox*>();
// 查找所有继承自QAbstractButton的控件(如QPushButton、QRadioButton)
QList<QAbstractButton*> buttons = this->findChildren<QAbstractButton*>();