Search code examples
c++qtsignalsslots

Undefined reference to SOME_SIGNAL_NAME


I have some signal in my class, is called requestFinished.
Also i have slot _finished, which should activate that signal.
But i have error undefined reference to MY_SIGNAL.
Here is _finished:

void VK::_finished(QNetworkReply *reply) {
    if (reply->error() != QNetworkReply::NoError) {
        qDebug() << (QString) reply->readAll();
    } else {
        QString json(reply->readAll());
        VKResponse *response = new VKResponse(json);
        VKError *error = new VKError(json);
        VKAnswer *answer = new VKAnswer(error, response);
        emit requestFinished(answer);
    }
}

Here is class VK:

class VK {
    public:
        VK(QString token);
        void request(QString method, std::map<QString, QString> data);
        ~VK();

    private:
        QString token;

    private slots:
        void _finished(QNetworkReply *reply);

    signals:
        void requestFinished(VKAnswer *answer);
};

As you can see, it contains method requestFinished in signals. What is my problem? Thanks.


Solution

  • Your VK class needs to publicly inherit QObject and include Q_OBJECT as the first thing:

    class VK: public QObject {
        Q_OBJECT
        public:
            VK(QString token);
            void request(QString method, std::map<QString, QString> data);
            virtual ~VK();
    
        private:
            QString token;
    
        private slots:
            void _finished(QNetworkReply *reply);
    
        signals:
            void requestFinished(VKAnswer *answer);
    };
    

    you will then need to ensure the moc is run against that (usually this is automatic in QtCreator)