Qt:没有这样的槽 QObject::SlotName(QNetworkReply*)
Qt : No such slot QObject::SlotName(QNetworkReply*)
我是 QT 的新手,我正在尝试连接到 REST 服务并获取数据。我能够成功连接到我机器上的本地 tomcat,但是我收到找不到插槽的错误。请让我知道我可能做错了什么,我在 Qt 方面没有那么多专业知识,所以请耐心等待
Person.cpp :
#include "person.h"
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include<QObject>
#include<QDebug>
#include<QNetworkReply>
Person::Person()
{
}
Person::~Person()
{
}
void Person::addPerson (){
QString url_str = "http://localhost:8080/";
Person *person = new Person;
person->setAccountStatus (true);
person->setFirstName ("Akshay");
person->setId (0);
person->setPassword ("helloWorld");
person->setUsername ("akshay@gmail.com");
QNetworkAccessManager *manager = new QNetworkAccessManager();
QObject object;
object.connect(manager,SIGNAL(finished(QNetworkReply*)),SLOT(slotRequestFinished(QNetworkReply*)));
QNetworkRequest request;
request.setUrl (url_str);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
QNetworkReply *reply = 0;
manager->get(request);
}
void Person::slotRequestFinished(QNetworkReply *reply)
{
qDebug()<<"Task finished";
}
Person.h :
#ifndef PERSON_H
#define PERSON_H
#include<QString>
#include<QObject>
#include<QNetworkReply>
class Person
{
private:
qint32 id;
QString username;
QString password;
QString firstName;
bool accountStatus;
public:
Person();
~Person();
qint32 getId() const;
void setId(const qint32 &value);
QString getUsername() const;
void setUsername(const QString &value);
QString getPassword() const;
void setPassword(const QString &value);
QString getFirstName() const;
void setFirstName(const QString &value);
bool getAccountStatus() const;
void setAccountStatus(bool value);
void addPerson();
public slots:
void slotRequestFinished(QNetworkReply *reply);
protected:
QList<Person> listPersons();
};
#endif // PERSON_H
}
我从 main.cpp 调用它:
Person *person = new Person;
person->addPerson ();
错误日志:
QML debugging is enabled. Only use this in a safe environment.
QObject::connect: No such slot QObject::slotRequestFinished(QNetworkReply*) in ../CheckNet/person.cpp:81
qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method
qt.network.ssl: QSslSocket: cannot resolve SSLv2_server_method
编辑:添加 QObject:
class Person : public QObject
{
Q_OBJECT
private :
// values, getters and setters
}
当我执行上述操作时,出现以下错误:
/home/akshay/Qtprojects/CheckNet/person.cpp:57: error: undefined reference to `vtable for Person'
/home/akshay/Qtprojects/build-CheckNet-Desktop_Qt_5_4_1_GCC_64bit-Debug/person.o:-1: In function `Person::~Person()':
简介
通过在新创建的 QObject
(在您的代码段中命名为 object
)上使用非静态成员函数 QObject::connect
,您实际上是在说所提供的 SLOT
是您当前调用成员函数的对象的一部分。
QObject object;
object.connect(...);
// ^- almost always wrong (deleteLater is the only slot in `QObject`)
根据上面的逻辑,我们可以很容易地看出您所描述的错误的原因; QObject
没有相关的广告位。
解决方案
要么你可以调用非静态 QObject::connect
,带三个参数,在真正具有所需 SLOT
的对象上(在你的情况下 Person
)。
例子
// .- make sure `this` belongs to an object that inherits `QObject`
// v
this->connect (
manager, SIGNAL(finished(QNetworkReply*)),
SLOT(slotRequestFinished(QNetworkReply*))
);
另一种选择是使用 QObject
中的 static 成员函数,其中第三个参数表示具有 SLOT
的对象:
例子
QObject::connect (
manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(slotRequestFinished(QNetworkReply*))
);
注意:上面的this
表示一个Person
类型的对象,需要的context是在一个non-static里面Person
的成员函数,例如代码段中的 addPerson
。
注意:记住对象作为sender和receiver[=68=传递] 必须继承自 QObject
,而 Person
目前没有。
我是 QT 的新手,我正在尝试连接到 REST 服务并获取数据。我能够成功连接到我机器上的本地 tomcat,但是我收到找不到插槽的错误。请让我知道我可能做错了什么,我在 Qt 方面没有那么多专业知识,所以请耐心等待
Person.cpp :
#include "person.h"
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include<QObject>
#include<QDebug>
#include<QNetworkReply>
Person::Person()
{
}
Person::~Person()
{
}
void Person::addPerson (){
QString url_str = "http://localhost:8080/";
Person *person = new Person;
person->setAccountStatus (true);
person->setFirstName ("Akshay");
person->setId (0);
person->setPassword ("helloWorld");
person->setUsername ("akshay@gmail.com");
QNetworkAccessManager *manager = new QNetworkAccessManager();
QObject object;
object.connect(manager,SIGNAL(finished(QNetworkReply*)),SLOT(slotRequestFinished(QNetworkReply*)));
QNetworkRequest request;
request.setUrl (url_str);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
QNetworkReply *reply = 0;
manager->get(request);
}
void Person::slotRequestFinished(QNetworkReply *reply)
{
qDebug()<<"Task finished";
}
Person.h :
#ifndef PERSON_H
#define PERSON_H
#include<QString>
#include<QObject>
#include<QNetworkReply>
class Person
{
private:
qint32 id;
QString username;
QString password;
QString firstName;
bool accountStatus;
public:
Person();
~Person();
qint32 getId() const;
void setId(const qint32 &value);
QString getUsername() const;
void setUsername(const QString &value);
QString getPassword() const;
void setPassword(const QString &value);
QString getFirstName() const;
void setFirstName(const QString &value);
bool getAccountStatus() const;
void setAccountStatus(bool value);
void addPerson();
public slots:
void slotRequestFinished(QNetworkReply *reply);
protected:
QList<Person> listPersons();
};
#endif // PERSON_H
}
我从 main.cpp 调用它:
Person *person = new Person;
person->addPerson ();
错误日志:
QML debugging is enabled. Only use this in a safe environment.
QObject::connect: No such slot QObject::slotRequestFinished(QNetworkReply*) in ../CheckNet/person.cpp:81
qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method
qt.network.ssl: QSslSocket: cannot resolve SSLv2_server_method
编辑:添加 QObject:
class Person : public QObject
{
Q_OBJECT
private :
// values, getters and setters
}
当我执行上述操作时,出现以下错误:
/home/akshay/Qtprojects/CheckNet/person.cpp:57: error: undefined reference to `vtable for Person'
/home/akshay/Qtprojects/build-CheckNet-Desktop_Qt_5_4_1_GCC_64bit-Debug/person.o:-1: In function `Person::~Person()':
简介
通过在新创建的 QObject
(在您的代码段中命名为 object
)上使用非静态成员函数 QObject::connect
,您实际上是在说所提供的 SLOT
是您当前调用成员函数的对象的一部分。
QObject object;
object.connect(...);
// ^- almost always wrong (deleteLater is the only slot in `QObject`)
根据上面的逻辑,我们可以很容易地看出您所描述的错误的原因; QObject
没有相关的广告位。
解决方案
要么你可以调用非静态 QObject::connect
,带三个参数,在真正具有所需 SLOT
的对象上(在你的情况下 Person
)。
例子
// .- make sure `this` belongs to an object that inherits `QObject`
// v
this->connect (
manager, SIGNAL(finished(QNetworkReply*)),
SLOT(slotRequestFinished(QNetworkReply*))
);
另一种选择是使用 QObject
中的 static 成员函数,其中第三个参数表示具有 SLOT
的对象:
例子
QObject::connect (
manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(slotRequestFinished(QNetworkReply*))
);
注意:上面的this
表示一个Person
类型的对象,需要的context是在一个non-static里面Person
的成员函数,例如代码段中的 addPerson
。
注意:记住对象作为sender和receiver[=68=传递] 必须继承自 QObject
,而 Person
目前没有。