我有一个创建 QPushButtons 的函数
void MainWindow::createButton(QFrame *parent, int x, int y, int w, int h, QString txt) {
QPushButton* button = new QPushButton(parent);
button->setGeometry(x,y,w,h);
button->setText(txt);
button->setObjectName("pushButton_" + txt);
vecButtons.push_back(button);
}
在主窗口构造函数中,我想连接按钮,因此我必须找到按钮。
有通用的搜索算法吗?
答案 0 :(得分:1)
即使您可以通过名称找到对象,例如:
QPushButton *button = parentWidget->findChild<QPushButton *>("pushButton_" + txt);
这种方法的缺点是您需要记录按钮的命名方式...
如果你按照评论中的建议去做会更好,在createButton方法中连接
void MainWindow::createButton(QFrame *parent, int x, int y, int w, int h, QString txt) {
QPushButton* button = new QPushButton(parent);
button->setGeometry(x,y,w,h);
button->setText(txt);
button->setObjectName("pushButton_" + txt);
connect(button, somesignal, this, someSlot); //here
vecButtons.push_back(button);
}