Solved QObject::connect: No such slot error
-
Hey,
I wrote the following code:void FunctionNodeDetailsForm::setUpEverything(){ QWidget *dataInterfaceWidget = new QWidget; dataInterfaceWidget->setLayout(new QHBoxLayout); // NAME LINE EDIT QLineEdit *nameLineEdit = new QLineEdit(); nameLineEdit->setText(di->displayName); dataInterfaceWidget->layout()->addWidget(nameLineEdit); connect(nameLineEdit, SIGNAL(textChanged(QString)), parentNode, SLOT(inputNameChanged(QString))); ui->inputs_widget->layout()->addWidget(dataInterfaceWidget); }
parentNode is a object of the class FunctionNode :
class FunctionNode : public Node { public: FunctionNode(GraphWidget *graphWidgetInput = nullptr, int numberInput = -1, FunctionNode *activeNodeInput = nullptr); public slots: void inputNameChanged(QString newName); };
The class Node derives from QObject and I call Q_Object macro in the Node-class
class Node : public QObject { Q_OBJECT ...
But at runtime, I get this error when setUpEverything() is called:
QObject::connect: No such slot Node::inputNameChanged(QString)
I also ran qmake, cleaned and rebuilt. I can't find more sources of the error.
Any ideas?
Thanks for answers! -
FunctionNode class needs Q_OBJECT too ...
-
This post is deleted! -
@mpergand
Oh, nice, it worked! I didn't know that and it does not make really sence to me yet...
I am interested, it would be really nice if someone could explain why I have to declare it in every specific class I want to use it, not only in a parent class.
Thanks! -
Hi,
Because without it, it's just a plain class. QObject based class with signals and slots needs to have
moc
run on them to generate additional code (the moc files) so Qt can do its magic with signals, slots, introspection etc. -
@SGaist
But it's derived from a class that is derived from QObject. Why don't do the child classes overtake the Q_Widget macro from it's parent? -
Because they are two different objects. That macro is not empty. It generates the static meta object that is needed for introspection and signal and slots to work among other things and again, it's needed by
moc
to properly do its work. -
@SGaist
Ok, thanks.
I was wondering, because I thought, that a child class gets also every macro called in the parent class, but it seems to be different. The preprocessor does not seem to add that code in the child classes. Is there a way to do this? I mean, with huge inheritence trees, maybe it's just too uncomfortable to add a needed macro in every class. -
Macros can't be inherited, they are placeholders that are replaced at compilation time. That said, trying to circumvent that is a bad idea. The
Q_OBJECT
macro also serves as a hint to the developer that this class is going to use some feature of the Qt Meta Object system e.g. a property, a signal, a slot etc. Also note that you only need that macro in classes where you'll be using one of these features, otherwise not.Removing that macro from everywhere, you would only make your code harder to understand.
-
@SGaist
Ok, sounds logical