【问题标题】:QT signal error: "this" is unavailable for static member functionQT 信号错误:“this”对于静态成员函数不可用
【发布时间】:2017-07-18 00:29:51
【问题描述】:

我正在为我的应用程序编写一个套接字类,它将向我介绍 QT 框架。当我尝试构建时,我收到此错误:'this' 对于静态成员函数不可用。 这是我的类 .h 和 .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;
    };
}

和 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 创建者将我带到 moc_instsocket.cpp(位于 build 文件夹中并指向此:

    // 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 类,因为只有一些调试消息。有人知道怎么解决吗?

【问题讨论】:

  • 您已将信号NotifyConnectedNotifyDisconnected 声明为static。不要认为这会奏效(而且几乎肯定不是你想要的)。
  • 效果很好。我试过这个,但一开始没有用……看起来我忘了清理我的项目。对于这个小错误,那是一篇很长的帖子。感谢您的帮助。

标签: qt c++11 qtcpsocket


【解决方案1】:

静态信号是什么意思?在 Qt 中,信号和槽用于对象级别。

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

从 QObject 或其子类之一(例如 QWidget)继承的所有类都可以包含信号和槽。当对象以可能对其他对象感兴趣的方式改变其状态时,信号由对象发出。这就是对象进行通信的全部内容。它不知道也不关心是否有任何东西在接收它发出的信号。这是真正的信息封装,并确保对象可以用作软件组件。 Signal Slots documentation

【讨论】:

  • 虽然这是对信号和插槽的一个很好的介绍性解释,但它并不能解释为什么 OP 会收到上述错误消息
  • 信号是静态的是错误的原因。还能怎么解释?
  • 答案中可以说明。好的答案应该是这样的
  • 好的。感谢您的反馈。
猜你喜欢
  • 1970-01-01
  • 2014-06-21
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
相关资源
最近更新 更多