无法识别继承类的插槽
Slot of inherited class not recognized
我正在尝试实现一个自定义的QTcpSocket
类,但似乎在运行时无法识别我的插槽我总是得到:
Object::connect: No such slot QTcpSocket::timeoutSlot()
这是我的代码:
我的标题:
#ifndef CUSTOM_SOCKET_H
#define CUSTOM_SOCKET_H
#include <QTcpSocket>
#include <QTimer>
class CustomSocket : public QTcpSocket {
Q_OBJECT
public:
CustomSocket(QObject* = 0);
private:
QTimer *mAuthTimeout;
public slots:
void timeoutSlot();
};
#endif
实现:
#include "customSocket.h"
CustomSocket::CustomSocket(QObject *aParent):QTcpSocket(aParent)
{
mAuthTimeout = new QTimer();
connect(mAuthTimeout, SIGNAL(timeout()), this, SLOT(timeoutSlot()));
mAuthTimeout->start(5000);
}
void CustomSocket::timeoutSlot(){
std::cout << "Timeout " << std::endl;
}
上面
引用的代码没有任何问题。
您收到的警告有 2 件奇怪的事情;
- 它抱怨QTcpSocket::timeoutSlot()。 我们不是在尝试连接到QTcpSocket,而是连接到CustomSocket。警告应该提到这一点。如果没有,则Q_OBJECT宏可能丢失。
- 如果我复制/粘贴代码并将其添加到空目录中,请添加main(),然后使用qmake -project; qmake; make。它工作得很好。没有警告。
查看 QTcpSocket 类,它是用Q_DISABLE_COPY宏声明的。这可能会导致您看到的错误。
无论情况是否如此,我认为您最好不要从 QTcpSocket 继承,而是在您的类中创建一个实例:-
class CustomSocket : public QObject
{
Q_OBJECT
private:
QTCPSocket* m_pSocket;
};
CustomSocket::CustomSocket(QObject* parent)
:QObject(parent)
{
m_pSocket = new QTcpSocket(this);
}
然后,您应该可以连接到 QTcpSocket 并使用其信号和插槽正常与其通信。
相关文章:
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何在没有信号的情况下从C++执行QML插槽
- 从Q_INVOKABLE功能或插槽中更改QQuick图像源
- 升压信号2将插槽传递到成员功能以断开连接
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- C++ 信号和插槽不工作:插槽不响应事件
- NS3 插槽混淆(需要帮助理解)
- QObject::连接无法将信号连接到*this*对象的插槽
- QT 插槽未在主线程上调用
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 控制带有信号/插槽的Qt QML滑动视图
- Qt 信号/插槽问题
- QT5 信号不会激活插槽内的功能
- Qt5:连接:如果插槽的参数少于信号,如何使用"连接"
- 需要实例化不同类/对象并在启动时确定的硬件插槽的设计模式
- 类继承自QLabel,为什么不调用自定义插槽?
- 信号/插槽基类多继承
- C /否这样的插槽 - 继承
- Qt UI 插槽不是由从基类继承的信号调用的
- 无法识别继承类的插槽