<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QNetworkAccessManager not emitting readyRead or finished  signal]]></title><description><![CDATA[<p dir="auto">hi, can anyone help to debug on this QNetworkAccessManager wrapper? I use a python flask application to receive on <a href="http://localhost:8000/" target="_blank" rel="noopener noreferrer nofollow ugc">http://localhost:8000/</a> and return 200, "hello world", but when I call <code>session.get(QUrl{"http://localhost:8000/"})</code>, it sucks on waiting for reply debug message and cannot go for no matter <code>QNetworkReply::readyRead</code> or <code>QNetworkReply::finished</code> signal, which cannot exit the loop.<br />
<img src="https://ddgobkiprc33d.cloudfront.net/57e64ef4-245e-42b7-a129-472f270b2f5d.png" alt="759e978e-632a-410a-b3fe-d93533dd502b-image.png" class=" img-fluid img-markdown" /><br />
<img src="https://ddgobkiprc33d.cloudfront.net/743c2221-c2ac-43cd-a1eb-df3bac22ce4f.png" alt="4477c33b-3e69-470b-9266-56de8f731104-image.png" class=" img-fluid img-markdown" /><br />
<strong>.h:</strong></p>
<pre><code>class session : public QObject {
		Q_OBJECT
	public:
		session(void);

		response get(QUrl url, QUrlQuery query = QUrlQuery(),
					 std::map&lt;QString, QVariant&gt; headers = std::map&lt;QString, QVariant&gt;(), uint timeout_sec = 60);
		response post(QUrl url, JSON json = JSON(), std::map&lt;QString, QVariant&gt; headers = std::map&lt;QString, QVariant&gt;(),
					  uint timeout_sec = 60);
		response post(QUrl url, QUrlQuery data = QUrlQuery(),
					  std::map&lt;QString, QVariant&gt; headers = std::map&lt;QString, QVariant&gt;(), uint timeout_sec = 60);

	signals:
		void request_start_signal(void);

	public slots:
		void request_process_slot(void);

	private:
		QNetworkAccessManager manager;
		QByteArray request_data;
		MethodsEnum request_method;
		bool is_finished = true;
		bool is_running = false;
		QNetworkRequest* request = nullptr;
		QNetworkReply* reply = nullptr;

		response wait_for_reply(uint timeout_sec);
	};
</code></pre>
<p dir="auto"><strong>.cpp</strong></p>
<pre><code>session::session(void) { connect(this, &amp;session::request_start_signal, this, &amp;session::request_process_slot); }

	void session::request_process_slot(void) {
		qDebug() &lt;&lt; "request slot";
		this-&gt;is_running = true;
		switch (request_method) {
			case GET: this-&gt;reply = this-&gt;manager.get(*this-&gt;request); break;
			case POST: this-&gt;reply = this-&gt;manager.post(*this-&gt;request, this-&gt;request_data); break;
			default: break;
		}
		QEventLoop loop;
		connect(this-&gt;reply, &amp;QNetworkReply::readyRead, &amp;loop, &amp;QEventLoop::quit);
		loop.exec();
		qDebug() &lt;&lt; "request slot finished";
		this-&gt;is_finished = true;
		this-&gt;is_running = false;
	}

	response session::wait_for_reply(uint timeout_sec) {
		uint64_t start_time = QDateTime::currentMSecsSinceEpoch();
		while (!this-&gt;is_finished) { // just do blocking request is fine for this project's case
			qDebug() &lt;&lt; "waiting for reply" &lt;&lt; (QDateTime::currentMSecsSinceEpoch() - start_time) / 1000;
			if (timeout_sec &gt; 0 &amp;&amp; QDateTime::currentMSecsSinceEpoch() - start_time &gt; timeout_sec * 1000) {
				qDebug() &lt;&lt; "request timeout";
				this-&gt;is_finished = true;
				this-&gt;reply-&gt;disconnect();
				delete this-&gt;reply;
				return response();
			}
			QThread::msleep(100);
		}
		response res = response(this-&gt;request, this-&gt;reply);
		this-&gt;reply-&gt;disconnect();
		delete this-&gt;request;
		this-&gt;request = nullptr;
		delete this-&gt;reply;
		this-&gt;reply = nullptr;
		return res;
	}

	response session::get(QUrl url, QUrlQuery query, std::map&lt;QString, QVariant&gt; headers, uint timeout_sec) {
		qDebug() &lt;&lt; "GET";
		while (!this-&gt;is_finished) {
			qDebug() &lt;&lt; "waiting for prev request to finish";
			QThread::msleep(100);
		}
		url.setQuery(query.query());
		if (this-&gt;request != nullptr) {
			delete this-&gt;request;
		}
		this-&gt;request = new QNetworkRequest(url);
		qDebug() &lt;&lt; 3;
		for (const auto&amp; [key, value] : headers) {
			this-&gt;request-&gt;setRawHeader(key.toUtf8(), value.toByteArray());
		}
		this-&gt;request_method = GET;
		this-&gt;is_finished = false;
		emit this-&gt;request_start_signal();
		return this-&gt;wait_for_reply(timeout_sec);
	}
</code></pre>
]]></description><link>https://forum.qt.io/topic/146106/qnetworkaccessmanager-not-emitting-readyread-or-finished-signal</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Nov 2026 09:59:28 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/146106.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 30 Jun 2023 08:06:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QNetworkAccessManager not emitting readyRead or finished  signal on Fri, 30 Jun 2023 08:55:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> @J-Hilk I have actually used another url which is <a href="https://accounts.spotify.com/api/token" target="_blank" rel="noopener noreferrer nofollow ugc">https://accounts.spotify.com/api/token</a>, this url can be successfully exceuted with the above code and get its return json, but the same code fails with <a href="http://localhost:8000/" target="_blank" rel="noopener noreferrer nofollow ugc">http://localhost:8000/</a> or <a href="https://api.spotify.com/v1/me/player/currently-playing" target="_blank" rel="noopener noreferrer nofollow ugc">https://api.spotify.com/v1/me/player/currently-playing</a></p>
]]></description><link>https://forum.qt.io/post/763147</link><guid isPermaLink="true">https://forum.qt.io/post/763147</guid><dc:creator><![CDATA[GaryL]]></dc:creator><pubDate>Fri, 30 Jun 2023 08:55:07 GMT</pubDate></item><item><title><![CDATA[Reply to QNetworkAccessManager not emitting readyRead or finished  signal on Fri, 30 Jun 2023 08:51:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/garyl">@<bdi>GaryL</bdi></a> said in <a href="/post/763144">QNetworkAccessManager not emitting readyRead or finished signal</a>:</p>
<blockquote>
<p dir="auto">so I don't know what is actually going on...</p>
</blockquote>
<p dir="auto">again, you're blocking any and all event handling in your qt application. The OS doesn't care about that and forwards your requests, thats why you may see stuff on the other side.</p>
<p dir="auto">Btw, the Pythonfyed c++ is hurting my soul :P, deeply</p>
]]></description><link>https://forum.qt.io/post/763146</link><guid isPermaLink="true">https://forum.qt.io/post/763146</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Fri, 30 Jun 2023 08:51:49 GMT</pubDate></item><item><title><![CDATA[Reply to QNetworkAccessManager not emitting readyRead or finished  signal on Fri, 30 Jun 2023 08:53:07 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/garyl">@<bdi>GaryL</bdi></a><br />
At this point I would suggest you dump calling your code, especially <code>wait_for_reply()</code>, for a while.  Just produce a minimal program without <code>while</code>, <code>is_finished</code>, <code>msleep</code>, <code>QEventLoop</code> etc.  Just connect the reply's <code>readyRead()</code> to something which outputs whatever it receives, do your <code>get()</code> or <code>post()</code> and verify you get the returned result (without "waiting" for it).</p>
]]></description><link>https://forum.qt.io/post/763145</link><guid isPermaLink="true">https://forum.qt.io/post/763145</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 30 Jun 2023 08:53:07 GMT</pubDate></item><item><title><![CDATA[Reply to QNetworkAccessManager not emitting readyRead or finished  signal on Fri, 30 Jun 2023 08:45:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> I am doing is because I original use qDebug() to print the variables of <code>this-&gt;reply</code> inside the loop so to find out what is going on, but it turns out that all variables seems normal. On the other hand, the python flask script do have receive a request and returned 200, "Hello world", so I don't know what is actually going on...</p>
]]></description><link>https://forum.qt.io/post/763144</link><guid isPermaLink="true">https://forum.qt.io/post/763144</guid><dc:creator><![CDATA[GaryL]]></dc:creator><pubDate>Fri, 30 Jun 2023 08:45:03 GMT</pubDate></item><item><title><![CDATA[Reply to QNetworkAccessManager not emitting readyRead or finished  signal on Fri, 30 Jun 2023 08:38:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/garyl">@<bdi>GaryL</bdi></a><br />
As the other two responders have said, you must not use either of</p>
<pre><code>while (!this-&gt;is_finished)
QThread::msleep(100)
</code></pre>
<p dir="auto">They go against how an event-driven framework like Qt works.</p>
]]></description><link>https://forum.qt.io/post/763140</link><guid isPermaLink="true">https://forum.qt.io/post/763140</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 30 Jun 2023 08:38:24 GMT</pubDate></item><item><title><![CDATA[Reply to QNetworkAccessManager not emitting readyRead or finished  signal on Fri, 30 Jun 2023 08:31:49 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> umm... not really understand, you mean sth like this?</p>
<pre><code>session::session(void) { connect(this, &amp;session::request_start_signal, this, &amp;session::request_process_slot); }

	void session::request_process_slot(void) {
		qDebug() &lt;&lt; "request slot";
		this-&gt;is_running = true;
		switch (request_method) {
			case GET: this-&gt;reply = this-&gt;manager.get(*this-&gt;request); break;
			case POST: this-&gt;reply = this-&gt;manager.post(*this-&gt;request, this-&gt;request_data); break;
			default: break;
		}
	}

	response session::wait_for_reply(uint timeout_sec) {
		while (this-&gt;reply == nullptr) {
			QThread::msleep(10);
		}
		QEventLoop loop;
		connect(this-&gt;reply, &amp;QNetworkReply::readyRead, &amp;loop, &amp;QEventLoop::quit);
		loop.exec();
		this-&gt;is_finished = true;
		response res = response(this-&gt;request, this-&gt;reply);
		this-&gt;reply-&gt;disconnect();
		delete this-&gt;request;
		this-&gt;request = nullptr;
		delete this-&gt;reply;
		this-&gt;reply = nullptr;
		return res;
	}

	response session::get(QUrl url, QUrlQuery query, std::map&lt;QString, QVariant&gt; headers, uint timeout_sec) {
		qDebug() &lt;&lt; "GET";
		while (!this-&gt;is_finished) {
			qDebug() &lt;&lt; "waiting for prev request to finish";
			QThread::msleep(100);
		}
		url.setQuery(query.query());
		if (this-&gt;request != nullptr) {
			delete this-&gt;request;
		}
		this-&gt;request = new QNetworkRequest(url);
		for (const auto&amp; [key, value] : headers) {
			this-&gt;request-&gt;setRawHeader(key.toUtf8(), value.toByteArray());
		}
		this-&gt;request_method = GET;
		this-&gt;is_finished = false;
		emit this-&gt;request_start_signal();
		return this-&gt;wait_for_reply(timeout_sec);
	}
</code></pre>
]]></description><link>https://forum.qt.io/post/763135</link><guid isPermaLink="true">https://forum.qt.io/post/763135</guid><dc:creator><![CDATA[GaryL]]></dc:creator><pubDate>Fri, 30 Jun 2023 08:31:49 GMT</pubDate></item><item><title><![CDATA[Reply to QNetworkAccessManager not emitting readyRead or finished  signal on Fri, 30 Jun 2023 08:24:34 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/garyl">@<bdi>GaryL</bdi></a> said in <a href="/post/763130">QNetworkAccessManager not emitting readyRead or finished signal</a>:</p>
<blockquote>
<p dir="auto">I have used a QEventLoop loop to block the process</p>
</blockquote>
<p dir="auto">No!</p>
<p dir="auto">Go back to the Qt event loop.</p>
]]></description><link>https://forum.qt.io/post/763132</link><guid isPermaLink="true">https://forum.qt.io/post/763132</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 30 Jun 2023 08:24:34 GMT</pubDate></item><item><title><![CDATA[Reply to QNetworkAccessManager not emitting readyRead or finished  signal on Fri, 30 Jun 2023 08:18:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> @J-Hilk</p>
<p dir="auto">Thanks for your help, but the problem is in function <code>request_process_slot</code>, not in function <code>wait_for_reply</code>.</p>
<p dir="auto">I have used a QEventLoop loop to block the process, and connect it with <code>connect(this-&gt;reply, &amp;QNetworkReply::readyRead, &amp;loop, &amp;QEventLoop::quit);</code>. but then not matter I use <code>QNetworkReply::readyRead</code> or <code>QNetworkReply::finished</code> as signal, it still sucks on this event loop.</p>
<p dir="auto">I have <code>connect(this, &amp;session::request_start_signal, this, &amp;session::request_process_slot);</code> in the constructor, so request_process_slot can be called when <code>emit this-&gt;request_start_signal();</code> in  get function.</p>
]]></description><link>https://forum.qt.io/post/763130</link><guid isPermaLink="true">https://forum.qt.io/post/763130</guid><dc:creator><![CDATA[GaryL]]></dc:creator><pubDate>Fri, 30 Jun 2023 08:18:09 GMT</pubDate></item><item><title><![CDATA[Reply to QNetworkAccessManager not emitting readyRead or finished  signal on Fri, 30 Jun 2023 08:13:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/garyl">@<bdi>GaryL</bdi></a> said in <a href="/post/763121">QNetworkAccessManager not emitting readyRead or finished signal</a>:</p>
<blockquote>
<p dir="auto">QThread::msleep(100)</p>
</blockquote>
<p dir="auto">no event handling, no signals, no slots being called</p>
<blockquote>
<p dir="auto">while (!this-&gt;is_finished)</p>
</blockquote>
<p dir="auto">same here</p>
]]></description><link>https://forum.qt.io/post/763128</link><guid isPermaLink="true">https://forum.qt.io/post/763128</guid><dc:creator><![CDATA[J.Hilk]]></dc:creator><pubDate>Fri, 30 Jun 2023 08:13:29 GMT</pubDate></item><item><title><![CDATA[Reply to QNetworkAccessManager not emitting readyRead or finished  signal on Fri, 30 Jun 2023 08:10:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/garyl">@<bdi>GaryL</bdi></a> said in <a href="/post/763121">QNetworkAccessManager not emitting readyRead or finished signal</a>:</p>
<blockquote>
<p dir="auto">QThread::msleep(100);</p>
</blockquote>
<p dir="auto">WTF?</p>
<p dir="auto">Qt is async so post your request, return to the event loop and when the answer arrives Qt will call your slot.</p>
]]></description><link>https://forum.qt.io/post/763125</link><guid isPermaLink="true">https://forum.qt.io/post/763125</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 30 Jun 2023 08:10:52 GMT</pubDate></item></channel></rss>