PDA

View Full Version : Hint: auto-connection of signals and slots when subclassing dialog widgets



Earthnail
17th April 2010, 17:13
I just stumbled over it: I have a widget A, which is sort of a container designer widget. It contains some standard elements, and a lot of functionality that is shared across a lot of dialogs I have.

So the obvious solution to me was: subclass A everytime you need a similar dialog and put all the common functionality in A.

To keep the form elements in A, I created an empty "centralWidget" in A. In B's constructor I did:


B::B(QWidget *parent) : A(parent) {
ui->setupUi(centralWidget);
}

This works fine, but auto-connection doesn't work anymore, i.e. void B::on_button1_clicked() was not called anymore.
Solution:


B::B(QWidget *parent) : A(parent) {
ui->setupUi(centralWidget);
QMetaObject::connectSlotsByName(this);
}

There you go. I hope I saved someone some time figuring this out.