QTcpSocket和插槽和信号的运行时错误

时间:2012-03-01 14:48:56

标签: qt signals-slots

我正在编写一些代码来建立TCP套接字,我在尝试设置我想要收听的信号时遇到错误。

代码是:

connection = new QTcpSocket(this);

connect(connection,SLOT(connected()),this,SIGNAL(onConnection()));
connect(connection,SLOT(readyRead()),this,SIGNAL(gotData()));
connect(connection,SLOT(disconnected()),this,SIGNAL(onConnection()));

我得到的错误是:

 Object::connect: Attempt to bind non-signal QTcpSocket::connected()
 Object::connect: Attempt to bind non-signal QTcpSocket::readyRead()
 Object::connect: Attempt to bind non-signal QTcpSocket::disconnected()

我找不到其他有同样问题的人。我在想,我正在做的事情是愚蠢的。我在该计划中的其他信号正在发挥作用。

1 个答案:

答案 0 :(得分:6)

你正在以糟糕的方式使用QObject :: connect方法!

你必须使用这样的连接方法:

connection = new QTcpSocket(this);

connect(connection,SIGNAL(connected()),this,SLOT(onConnection()));
connect(connection,SIGNAL(readyRead()),this,SLOT(gotData()));
connect(connection,SIGNAL(disconnected()),this,SLOT(onConnection()));

有关详细信息,请转到here