[SOLVED] function not found?
-
Hey all,
I am trying to have my class emit a custom signal when clicked, but whenever I call connect() it doesn't work. The console tells me "No such slot NBhistoryAction::emitHistoryRequest() in ../NovaBrowser_v8/nbhistory.cpp:7". How is this possible? emitHistoryRequest is in the code.
Here is all the code:
@
class NBhistoryAction : public QAction
{
Q_OBJECT
public:
explicit NBhistoryAction(QWidget *parent = 0);
void setHistoryItem(QString item);
void emitHistoryRequest();private:
QString historyItem;signals:
QString historyRequest(QString Request);
};
@@
NBhistoryAction::NBhistoryAction(QWidget *parent)
: QAction(parent)
{
connect(this, SIGNAL(triggered()), SLOT(emitHistoryRequest()));
}void NBhistoryAction::setHistoryItem(QString item)
{
historyItem = item;
}void NBhistoryAction::emitHistoryRequest()
{
qDebug() << "wow it worked!";
QString request = this->historyItem;
emit historyRequest(request);
}
@Thanks!
-
NBhistoryAction::emitHistoryRequest() is not declared as a slot therefore there is no slot called emitHistoryRequest() to connect to.
@
class NBhistoryAction : public QAction
{
Q_OBJECT
public:
explicit NBhistoryAction(QWidget *parent = 0);
void setHistoryItem(QString item);private slots: // or public if that makes sense
void emitHistoryRequest();private:
QString historyItem;signals:
QString historyRequest(QString Request);
};
@ -
Typo? emitHistoryRequest() isn't labeled as a slot in the class definition.
-
It's in the code, but it's not a slot.
You need to put it under "public slots:", not "public:".
(In fact, it should probably be private, not public)
EDIT: Oops, too slow ;)
-
Thank you both, that fixed it!
Now for a Qt/c++ theory question:
whats the difference between 'public' and 'public slots'? why can setHistoryItem() be in the 'public' area while emitHistoryRequest() has to be in the 'public slots' area? -
"moc":http://qt-project.org/doc/qt-5/moc.html uses the slots annotation to detect that a function should be added to the meta-object, which is in turn used to handle signal to slot connections, and signal delivery.
In the code snippet presented, setHistoryItem() isn't used, making it difficult to tell if it will be accessible when desired.
-
[quote author="nicky j" date="1403252619"]whats the difference between 'public' and 'public slots'? why can setHistoryItem() be in the 'public' area while emitHistoryRequest() has to be in the 'public slots' area?[/quote]connect(this, SIGNAL (triggered()), SLOT (emitHistoryRequest()) );
Only "public/protected/private slots" can be put inside the SLOT () macro
-
A side note: with the c++11 syntax of connect, that doesn't use SIGNAL and SLOT macros you can connect to any non-slot member as well:
@
connect(this, &NBhistoryAction::triggered, this, &NBhistoryAction::emitHistoryRequest);
@ -
That pointer to member function syntax, although widely referred to as the c++11 syntax, "doesn't require c++11.":http://woboq.com/blog/new-signals-slots-syntax-in-qt5.html
-
Well yeah, that's true. I call it that because I use it extensively with lambdas, which are c++11. But you're 100% right.