I’m using QT creator for creating the Ui form. I’ve a base form which contains 5 buttons (testListForm). I’m inheriting this base class in my derived class (DisplayTestResultsForm) via TestStatusForm. when i click on the button in the derived class, the function on_pushbutton_1_clicked() getting called thrice.
below is the code snippet
class TestListForm : public TestBaseForm
{
Q_OBJECT
private slots:
virtual void on_pushButton_1_clicked();
virtual void on_pushButton_2_clicked();
virtual void on_pushButton_3_clicked();
virtual void on_pushButton_4_clicked();
virtual void on_pushButton_5_clicked();
}
class TestStatusForm : public TestListForm
{
Q_OBJECT
....
};
class DisplayTestResultsForm : public TestStatusForm
{
Q_OBJECT
private slots:
void on_pushButton_1_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
}
When i call on_pushButton_1_clicked in DisplayTestResultsForm, i’m getting this function getting called thrice. I’m not making any explicit connect call as Ui will take care of this using ConnectSlotByName.
Can anyone suggest what could be the problem?
Thanks in advance.
5
There are two mistakes:
-
Private slots are local. There’s no point in making them virtual, as no derived class sees them. You must make the slots protected.
-
When you have virtual slots, you must only declare them to moc once. The
slots
macro is empty and only has meaning to the moc tool. The most base class that declares the slots should have them declared as slots. All of the derived classes must not declare them as slots, but merely asQ_DECL_OVERRIDE
reimplementations.
Thus:
class TestListForm : public TestBaseForm
{
Q_OBJECT
protected slots: // protected, not private
virtual void on_pushButton_1_clicked();
virtual void on_pushButton_2_clicked();
virtual void on_pushButton_3_clicked();
virtual void on_pushButton_4_clicked();
virtual void on_pushButton_5_clicked();
}
class TestStatusForm : public TestListForm
{
Q_OBJECT
protected: // protected, overriden, no slots macro
void on_pushButton_1_clicked() Q_DECL_OVERRIDE;
void on_pushButton_2_clicked() Q_DECL_OVERRIDE;
void on_pushButton_3_clicked() Q_DECL_OVERRIDE;
void on_pushButton_4_clicked() Q_DECL_OVERRIDE;
void on_pushButton_5_clicked() Q_DECL_OVERRIDE;
}
3