qt代码创建的控件如何通过控件名称自动关联信号)
问题
在类的构造中新建了一个button,希望它能自动关联到类中的void on_hideBtn_clicked()函数,而不需要使用connect()函数。
代码
ClassName::ClassName
{
QPushButton* pushbutton = new QPushButton(tr("hide"));
}
void ClassName::on_hideBtn_clicked()
{
...
}
解决方案
new对象后添加如下两句话:
pushbutton->setObjectName("hideBtn");
QMetaObject::connectSlotsByName(this);