Unsolved Error when using simple API
-
Hey everyone,
I'm just playing around with a simple API and trying to connect it to qt for a little project. I've written the same code before, but instead of just a function, I had it in a pushButton function.
I'm getting an error saying:terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)This is the code:
void Widget::connectAPI() { qDebug() << "Beginning to connect"; QNetworkAccessManager *man = new QNetworkAccessManager(this); connect(man, &QNetworkAccessManager::finished, this, &Widget::imageDownloadFinished); QUrl url = QUrl(imgUrl); QNetworkRequest request(url); man->get(request); qDebug() << "Finished Connection1"; } void Widget::imageDownloadFinished(QNetworkReply *reply) { qDebug() << "Finished Connection"; QString replynet = reply->readAll(); std::string replynetstd = replynet.toUtf8().constData(); replynetstd.replace(0, 11, ""); splitter(replynetstd); qDebug() << "Beginning image function"; }
So when I did it before (and it worked), connectAPI() was void::void Widget::on_pushButton_clicked();
Thanks in advance for the help
-
@Alfie-Anil said in Error when using simple API:
replynetstd.replace(0, 11, "");
you should make sure that your string is long enough...
-
@Christian-Ehrlicher Sorry should have mentioned this before but the program stops after the connectAPI() ends. e.g. the final output is "Finished Connection"
-
Apart from the fact that you create a new QNetworkManager instance every time for no reason (and leaking it) I don't see why it should stop there. Therefore use a debugger and see where it really asserts.
-
Hi,
That because reply in that function is an invalid pointer. You do not assign it the return value of your get call.
-
hi @SGaist I'm not completely sure what you mean. Do you think you could draft what the code should look like?
-
You have a class member called reply, correct ?
Where in your code do you assign it something valid ? -
Hey @SGaist Thanks for the help.
Unfortunately after some more testing I found that the problem was not there. The problem is that later in the function, I convert the bytearray into a QPixmap and add it to a vector. And in the widget.cpp file I use label->setPixmap(imagevector.at(0)).
The problem with this is that the label and buttons have all been loaded before the function finishes. So do you know if there is a way to get around that please?
Would a sleep function be the most sufficient and efficient way? Thanks -
@Alfie-Anil said in Error when using simple API:
The problem with this is that the label and buttons have all been loaded before the function finishes.
What do you mean with this? Properly initialize your members in the ctor (e.g. with nullptr) and all is fine.