信号不被发射
我有一个简单的类 Networking :
private:
QNetworkAccessManager *httpclient;
我在构造函数中创建一个对象并连接信号和槽:
httpclient = new QNetworkAccessManager(this);
connect(httpclient, SIGNAL(finished(QNetworkReply*)), this, SLOT(httpRequestFinished(QNetworkReply*)));
现在我将通过公共方法 getPage 调用 QNetworkAccessManager 的 get 方法:
void Networking::getPage(const QString &uri)
{
QNetworkRequest request;
request.setUrl(uri);
httpclient->get(request);
}
我希望 >httpRequestFinished 被调用,但事实并非如此。 我还出于测试目的尝试了这个:
void Networking::getPage(const QString &uri)
{
QNetworkRequest request;
request.setUrl(uri);
reply = httpclient->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(httpReplyFinished()));
}
其中 reply 在文件 networking.h 中正确定义:
private:
QNetworkAccessManager *httpclient;
QNetworkReply *reply;
没有任何改变!但是,如果我在 get-Request 之后访问 reply->bytesAvailable(),所有信号都会同时发出!我现在正在搜索几个小时,但不知道我做错了什么。
I have a simple class Networking with a:
private:
QNetworkAccessManager *httpclient;
I create an object in the constructor and connect signal and slot:
httpclient = new QNetworkAccessManager(this);
connect(httpclient, SIGNAL(finished(QNetworkReply*)), this, SLOT(httpRequestFinished(QNetworkReply*)));
Now I am going to call the QNetworkAccessManager's get method via a public method getPage:
void Networking::getPage(const QString &uri)
{
QNetworkRequest request;
request.setUrl(uri);
httpclient->get(request);
}
I expect that httpRequestFinished gets called, but it's not.
I also tried it this for testing purposes:
void Networking::getPage(const QString &uri)
{
QNetworkRequest request;
request.setUrl(uri);
reply = httpclient->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(httpReplyFinished()));
}
where reply is properly defined in file networking.h:
private:
QNetworkAccessManager *httpclient;
QNetworkReply *reply;
Nothing changed! But if I access reply->bytesAvailable() after the get-Request all signals are emitted at the same time! I'm searching now for hours but don't know what I am doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以...我找到了原因 - 但不完全是:这是内存管理。
我必须在我的主窗口类中将我的对象定义为指针
现在我可以使用它 - 例如在我的按钮操作中:
一切都按预期进行!
乌夫..有很多东西要学
so... i found out why - but not exactly: it was memory management.
I had to define my object as pointer in my mainwindow class
Now i can use it - for example in my pushbutton-action:
All went as expected!
Ufff.. Much to learn