信号不被发射

发布于 2024-08-20 09:26:18 字数 1158 浏览 10 评论 0原文

我有一个简单的类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂人 2024-08-27 09:26:18

所以...我找到了原因 - 但不完全是:这是内存管理。
我必须在我的主窗口类中将我的对象定义为指针

Networking *nw;

现在我可以使用它 - 例如在我的按钮操作中:

nw = new Networking();
nw->getPage("my url here");

一切都按预期进行!
乌夫..有很多东西要学

so... i found out why - but not exactly: it was memory management.
I had to define my object as pointer in my mainwindow class

Networking *nw;

Now i can use it - for example in my pushbutton-action:

nw = new Networking();
nw->getPage("my url here");

All went as expected!
Ufff.. Much to learn

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文