Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I wrote earlier in my comment, there are several issues with this code, but the main issue is the last one, so if you do not care about other suggestions, jump to that one.</p> <p>1) QVector replies;</p> <p>You should consider using <code>QStringList</code> here.</p> <p>2) QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(get_reply_error(QNetworkReply::NetworkError))); QByteArray res = reply->readAll();</p> <p>You should do this in the constructor alongside the finished signal management.</p> <p>3) QNetworkAccessManager *manager;</p> <p>It is unnecessary to allocate this object on the heap, and it also has additional burden like constructing manually in the class constructor, and deleting it manually in the class destructor. You could simply allocate this object on the stack without issues, and that would result a slightly simpler code.</p> <p>4) QByteArray res = reply->readAll();</p> <p>You probably do not need a temporary variable in here.</p> <p>5) QString data = res.data();</p> <p>You need to make sure about the encoding, so I would suggest writing something like this:</p> <p>QString data = QString::fromUtf8(reply->readAll());</p> <p>or</p> <p>QString data = QString::fromLatin1(reply->readAll());</p> <p>or</p> <p>QString data = QString::fromLocal8Bit(reply->readAll());</p> <p>6) replies.push_back(data);</p> <p>This is not Qt style. You could consider this:</p> <p>replies.append(data);</p> <p>7) w.ret_out(buf);</p> <p>You are asking for this before entering the Qt event loop. You should write the ui element with setText() when your slot handler is called for finished. Try this:</p> <pre><code>void Requester::get_reply(QNetworkReply *reply) { QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(get_reply_error(QNetworkReply::NetworkError))); QByteArray res = reply-&gt;readAll(); QString data = res.data(); replies.push_back(data); QObject::disconnect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(get_reply_error(QNetworkReply::NetworkError))); reply-&gt;deleteLater(); ui-&gt;out-&gt;setText(data); // You will need access the "ui" here obviously, or the main window needs access to the Requester object, and the signal-slot has to be connected in there. } </code></pre> <p>You could also set up a "local" <code>QEventLoop</code> by instantiating it, but it is better if you leave it with the main application event loop, namely <code>return a.exec();</code>.</p> <p>Note that the QtNetwork API is async, and it needs an event loop to work. It is not blocking because that would be unfortunate for UI applications without additional worker thread management for end users. That is the reason why the mysterious <code>QDialog::exec()</code> code "fixes" your code because that is an event loop right there. However, that seems to be a not so good fix in this special case, so I would suggest to use the aforementioned main application event loop like probably the vast majority of the applications out there in such scenarios like this.</p> <p>In your newly uploaded code, there are two issues:</p> <p>...MainWindow, SLOT(MainWindow::ret_out(QString))...</p> <p>1) You do not seem to have a MainWindow object on the heap anywhere available for your requester class.</p> <p>2) MainWindow::ret_out(QString) is wrong... just like you do not use the other slots like that either. You should drop the MainWindow:: scope in there...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload