Solved QT Looking for Signal in parent class, rather than derived class.
-
I have a ButtonPanel widget that I use as a base widget for a couple of different ButtonPanels with different configuration.
In each derived class, I am trying to connect signals for that particular class, but when I try to connect the signals, I get an error:
QObject::connect: No such signal ButtonPanel::mostRecentPatientBtn_Clicked() in ..welcomebuttonpanel.cpp:6
So it seems QT is looking for the signal in ButtonPanel, the parent class, and not finding the signal in WelcomeButtonPanel, the derived class.
The base class is ButtonPanel:
Header:
#ifndef BUTTONPANEL_H #define BUTTONPANEL_H #include <QPushButton> #include <QVBoxLayout> #include "customwidget.h" class ButtonPanel : public CustomWidget { Q_OBJECT public: explicit ButtonPanel(QWidget *parent = nullptr); signals: public slots: }; #endif // BUTTONPANEL_H
Source:
#include "buttonpanel.h" #include "uiutils.h" ButtonPanel::ButtonPanel(QWidget *parent) : CustomWidget(parent) { this->setStyleSheet("QPushButton {font: bold 22px 'Arial'; color: white; background-color: navy;" "border: 3px solid #232323;" "border-radius: 15px;}" "QPushButton#powerOffBtn {background: red;}"); //other unrelated code here }
The derived class is WelcomeButtonPanel:
Header
#ifndef WELCOMEBUTTONPANEL_H #define WELCOMEBUTTONPANEL_H #include "buttonpanel.h" class WelcomeButtonPanel : public ButtonPanel { public: WelcomeButtonPanel(); signals: void mostRecentPatientBtn_Clicked(); void newPatientBtn_Clicked(); void managePatientsBtn_Clicked(); void settingsBtn_Clicked(); void powerOffBtn_Clicked(); private: }; #endif // WELCOMEBUTTONPANEL_H
Source:
#include "welcomebuttonpanel.h" WelcomeButtonPanel::WelcomeButtonPanel() { QPushButton *mostRecentPatientBtn = new QPushButton("Most Recent\nPatient"); connect(mostRecentPatientBtn, SIGNAL(clicked()), this, SIGNAL(mostRecentPatientBtn_Clicked())); QPushButton *newPatientBtn = new QPushButton("New\nPatient"); connect(newPatientBtn, SIGNAL(clicked()), this, SIGNAL(newPatientBtn_Clicked())); QPushButton *managePatientsBtn = new QPushButton("Manage\nPatients"); connect(managePatientsBtn, SIGNAL(clicked()), this, SIGNAL(managePatientsBtn_Clicked())); QPushButton *settingsBtn = new QPushButton("Settings"); connect(settingsBtn, SIGNAL(clicked()), this, SIGNAL(settingsBtn_Clicked())); QPushButton *emptyBtn = new QPushButton; QPushButton *powerOffBtn = new QPushButton("Power\nOff"); connect(powerOffBtn, SIGNAL(clicked()), this, SIGNAL(powerOffBtn_Clicked())); mostRecentPatientBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); newPatientBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); managePatientsBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); settingsBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); emptyBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); powerOffBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); QVBoxLayout *layout = new QVBoxLayout; layout->setMargin(15); layout->setSpacing(15); layout->addWidget(mostRecentPatientBtn, 1); layout->addWidget(newPatientBtn, 1); layout->addWidget(managePatientsBtn, 1); layout->addWidget(settingsBtn, 1); layout->addWidget(emptyBtn, 1); layout->addWidget(powerOffBtn, 1); emptyBtn->hide(); this->setLayout(layout); }
In this code, I try to connect each button clicked signal to a WelcomeButtonPanel signal as described here. However, I get errors on all the lines that try to connect the slots with No such signal ButtonPanel::mostrecentPatientBtn_Clicked(), so it is obviously not finding the slot in WelcomeButtonPanel.
How can I fix this issue?
-
Hi,
First thing I can see is that you are missing the Q_OBJECT macro in your class.
Don’t forget to re-run qmake after adding it and before building your application.
-
Hi,
First thing I can see is that you are missing the Q_OBJECT macro in your class.
Don’t forget to re-run qmake after adding it and before building your application.
-
@SGaist facepalm Well, you were right. I included it in the base class, where I didn't need it, and not in the derived class, where I do need it, and ended up reading too far into the error message to realize.
Thank you!