2017-02-27 399 views
0

我正在爲我的應用程序的套接字類工作,這將在QT框架中介紹我。當我嘗試構建時,出現此錯誤:「this」對靜態成員函數不可用。 這是我班的.h和.cppQT信號錯誤:「this」不可用於靜態成員函數

#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; 
    }; 
} 

和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); 
    } 
} 

,這是錯誤:

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); 

當我點擊他們,Qt Creator的花我moc_instsocket.cpp(即在建立文件夾和poit到這:

// 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); 
} 

我不知道該怎麼辦,雖然我檢查了所有的代碼幾次。由於只有一些調試消息,因此不需要知道utils類。有誰知道如何解決它?

+1

聲明瞭signals'NotifyConnected'和'NotifyDisconnected'是'static'。不要以爲這會起作用(而且幾乎肯定不是你想要的)。 –

+0

這很好。我嘗試過,但最初並沒有工作......看起來像我忘了清理我的項目。這是一個很長的帖子,因爲這個小小的錯誤。感謝幫助。 – Jarlio

回答

-1

靜態信號是什麼意思?在Qt中,信號和插槽用於對象級別。

signals: 
    static void NotifyConnected(); 
    static void NotifyDisconnected(); 

All classes that inherit from QObject or one of its subclasses (e.g., QWidget) can contain signals and slots. Signals are emitted by objects when they change their state in a way that may be interesting to other objects. This is all the object does to communicate. It does not know or care whether anything is receiving the signals it emits. This is true information encapsulation, and ensures that the object can be used as a software component. Signal Slots documentation

+0

儘管這是對信號和插槽的很好的介紹性解釋,但它並不能解釋OP爲什麼會收到上述錯誤信息 – RobbieE

+0

信號是靜態的是錯誤的原因。還有什麼可以解釋的? –

+0

它可以在答案中解釋。這就是好的答案應該看起來像 – RobbieE