lundi 27 février 2017

QT signal error: "this" is unavailable for static member function

I'm working at a socket class for my application that will introduce me in QT framework. When I try to build I get this error: 'this' is unavailable for static member functions. This is my class .h and .cpp

#pragma once
#include <QObject>
class QTcpSocket;
namespace Ps{
    class InstSocket : public QObject
    {
        Q_OBJECT
    public:
        InstSocket(QObject *parent=0);
        bool Connect();
        bool isOpen();
        void Disconnect();

        //Geters
        QString GetHostName() const {return m_hostName;}
        quint16 GetPort() const {return m_port;}
        //seters
        void SetHostName(const QString& value);
        void SetPort(quint16 value);
        void SetLongWaitMs(int value){m_longWaitMs = value;}
        void SetShortWaitMs(int value){m_shortWaitMs = value;}
        void WriteData(const QString &data) const;

        ~InstSocket();
        QString ReadData() const;
    signals:
        static void NotifyConnected();
        static void NotifyDisconnected();

    private slots:
        void onConnected();
        void onDisconnected();

    private:
        //this holds a reference to QtcpSocket
        QTcpSocket& m_socket;
        QString m_hostName;
        quint16 m_port;
        int m_shortWaitMs;
        int m_longWaitMs;

        explicit InstSocket(const InstSocket& rhs) = delete;
        InstSocket& operator= (const InstSocket& rhs) = delete;
    };
}

and the cpp:

#include "instsocket.h"
#include "QTcpSocket"
#include "QDebug"
#include "utils.h"

namespace Ps
{
    InstSocket::InstSocket(QObject *parent) :
        QObject(parent),
        m_socket(*new QTcpSocket(this)),
        m_hostName(""),
        m_port(0),
        m_shortWaitMs(0),
        m_longWaitMs(0)
    {
        /* my signals are wired to the undelying socket signals, the signal connected is triggered, when a conection
         * is established. This will be wired to onConnected and Disconnected slots*/
        connect(&m_socket, &QTcpSocket::connected, this, &InstSocket::onConnected);
        connect(&m_socket, &QTcpSocket::disconnected, this, &InstSocket::onDisconnected);
    }

    bool InstSocket::Connect()
    {

        qDebug() << "attempting to connect to "<< m_hostName << "on port" << m_port << "with wait time: "<<m_longWaitMs;
        m_socket.connectToHost(m_hostName, m_port, QTcpSocket::ReadWrite);
        return m_socket.waitForConnected(m_longWaitMs);
    }

    bool InstSocket::isOpen()
    {
        return m_socket.isOpen();
    }

    void InstSocket::Disconnect()
    {
        if(!isOpen()) return;
        m_socket.disconnectFromHost();
    }


    void InstSocket::onConnected()
    {
        emit NotifyConnected();
    }

    void InstSocket::onDisconnected()
    {
        emit NotifyDisconnected();
    }

    void InstSocket::SetHostName(const QString &value)
    {
        m_hostName = value;
    }

    void InstSocket::SetPort(quint16 value)
    {
        m_port = value;
    }

    void InstSocket::WriteData(const QString &data) const
    {
        /*support for writeing to socket. The write metod of the socket will return the number of bites writen*/
        int bytes_written = m_socket.write(qPrintable(data));
        qDebug() << "Bytes written: "<<bytes_written;
    }

    QString InstSocket::ReadData() const
    {
        if(!m_socket.isReadable())
        {
            return "ERROR: Socket is unreadable.";
        }
        QString result;
        //until the socket reports there is no data available
        while(!m_socket.atEnd())
        {
            result.append(m_socket.readAll());
            /*since typically a PC would be much faster at reading than an instrument might be at writing
             * instrument must have a chance to queue up more data in case the message it's sending us is long.*/
            m_socket.waitForReadyRead(m_shortWaitMs);

        }
        return result;
    }

    InstSocket::~InstSocket()
    {
        Utils::DestructorMsg(this);
    }
}

and this is the error:

Qt Projects\build-Vfp-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug\debug\moc_instsocket.cpp:-1: In static member function 'static void         Ps::InstSocket::NotifyConnected()':
    error: 'this' is unavailable for static member functions

QMetaObject::activate(this, &staticMetaObject, 0, Q_NULLPTR); In static member function 'static void Ps::InstSocket::NotifyDisconnected()':
error: 'this' is unavailable for static member functions
    QMetaObject::activate(this, &staticMetaObject, 1, Q_NULLPTR);

When I clicked on them, QT creator took me to moc_instsocket.cpp (that is in build folder and poit to this:

    // SIGNAL 0
void Ps::InstSocket::NotifyConnected()
{
    QMetaObject::activate(this, &staticMetaObject, 0, Q_NULLPTR);
}

// SIGNAL 1
void Ps::InstSocket::NotifyDisconnected()
{
    QMetaObject::activate(this, &staticMetaObject, 1, Q_NULLPTR);
}

I can't figure out what to do althought I checked all the code several times. There is no need to know about utils class since there are just some debug messages. Did anyone know how to fix it?

Aucun commentaire:

Enregistrer un commentaire