2017-02-27 10 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 작성자가했다

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

me to moc_instsocket.cpp (빌드 폴더에 있고 이에 대한 내용입니다 :

// 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

당신은 static''가되도록 signals'NotifyConnected'과'NotifyDisconnected'을 선언했습니다. 그게 효과가 있다고 생각하지 마십시오 (그리고 어쨌든 당신이 원하는 것이 거의 확실하지 않습니다). –

+0

잘 작동합니다. 나는 이것을 시도했다. 그러나 그것은 처음에 일을 didnt한다. .. 그 다음 나는 나의 프로젝트를 깨끗하게하는 것을 잊는다. 그것은이 작은 오류에 대한 긴 게시물이었습니다. 도와 주셔서 감사합니다. – 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