Qt中通过代码设置控件的objectName,和通过objectName查找该控件

时间:2025-03-08 07:45:40

(1)在代码中,动态创建的一些控件,先通过setObjectName(“XXX”);
用QDesigner创建的界面,就已经设置了setObjectName
(2)再通过findChild

QLabel *macLabel = new QLabel(this);
macLabel->setObjectName("mac");

//查找这个控件的时候
QLabel *macLabel = yourWidget->findChild<QLabel*>("mac");
qDebug() << macLabel->text();

参考:
(1)/weixin_38293850/article/details/80251446
(2)https:///kf/201712/