Solved QNetworkAccessManager in loop ?
-
I need to download images to setPixmap of QLabels.
Only one is OK, more than one have no effort.
How to use QNetworkAccessManager in loop ?void FormFind::search() { QString sql="select * from user where name like '%" + ui->lineEdit->text() + "%'"; bool b=query.exec(sql); while(query.next()) { int ids = query.value("id").toInt(); QString name = query.value("name").toString(); QString sex = query.value("sex").toString(); QString urla=query.value("avantar").toString(); itemfind=new ItemFind; itemfind->ui->labelAccount->setText(QString::number(ids)); itemfind->ui->labelName->setText(name); itemfind->ui->labelSex->setText(sex); itemfind->ui->labelDomicile->setText(domicile); QListWidgetItem *LWI = new QListWidgetItem(ui->listWidget); ui->listWidget->addItem(LWI); ui->listWidget->setItemWidget(LWI,itemfind); LWI->setSizeHint(QSize(140,70)); QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyPixmap(QNetworkReply*))); manager->get(QNetworkRequest(QUrl(urla))); } } void FormFind::replyPixmap(QNetworkReply* reply) { qDebug() << "reply" << reply->error(); QPixmap pixmap; pixmap.loadFromData(reply->readAll()); itemfind->ui->labelAvantar->setPixmap(pixmap.scaled(50,50)); }
-
As you have it at the moment you are creating every time a QNetworkAccessManager. This is not required. See third paragraph in detailed description it says:
Once a QNetworkAccessManager object has been created, the application can use it to send requests over the network.You can basically fire a number of request to the same QNetworkAccessManager. Only with large numbers you probably need to check, if this is possible. Also the sequence of replies may not be identical to the sequence of your actual replies AFAIK.
You might want tocheck also how to delete the replies after your processing. Checkout the notes between those examples in the detailed description. Use deleteLater for teh replies.
-
I found a solution: package net request and reply in sub item, trigger them in parent with function.
void FormFind::search() { QString sql="select * from user where name like '%" + ui->lineEdit->text() + "%'"; bool b=query.exec(sql); while(query.next()) { int ids = query.value("id").toInt(); QString name = query.value("name").toString(); QString sex = query.value("sex").toString(); QString urla=query.value("avantar").toString(); ItemFind *itemfind=new ItemFind; itemfind->ui->labelAccount->setText(QString::number(ids)); itemfind->ui->labelName->setText(name); itemfind->ui->labelSex->setText(sex); itemfind->ui->labelDomicile->setText(domicile); QListWidgetItem *LWI = new QListWidgetItem(ui->listWidget); ui->listWidget->addItem(LWI); ui->listWidget->setItemWidget(LWI,itemfind); LWI->setSizeHint(QSize(140,70)); itemfind->getPixmap(urla); // trigger } } void ItemFind::getPixmap(QString url) { QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyPixmap(QNetworkReply*))); manager->get(QNetworkRequest(QUrl(url))); } void ItemFind::replyPixmap(QNetworkReply* reply) { QPixmap pixmap; pixmap.loadFromData(reply->readAll()); ui->labelAvantar->setPixmap(pixmap.scaled(60,60)); }
-
Glad to read that you have solved your issue. Thanks for providing a potential solution for others.
-
@sonichy its working but is not optimised. You can prevent creating many objects by using
QNetworkReply
and C++11 lambda as follow:void FormFind::search() { QString sql="select * from user where name like '%" + ui->lineEdit->text() + "%'"; bool b=query.exec(sql); QNetworkAccessManager *manager = new QNetworkAccessManager(this); while(query.next()) { int ids = query.value("id").toInt(); QString name = query.value("name").toString(); QString sex = query.value("sex").toString(); QString urla=query.value("avantar").toString(); itemfind=new ItemFind; itemfind->ui->labelAccount->setText(QString::number(ids)); itemfind->ui->labelName->setText(name); itemfind->ui->labelSex->setText(sex); itemfind->ui->labelDomicile->setText(domicile); QListWidgetItem *LWI = new QListWidgetItem(ui->listWidget); ui->listWidget->addItem(LWI); ui->listWidget->setItemWidget(LWI,itemfind); LWI->setSizeHint(QSize(140,70)); QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(urla))); connect(reply, &QNetworkReply::finished, [=, &reply, &itemfind]() { //handle http redirection (code 302) if you like as described in // https://forum.qt.io/topic/50516/qnetworkreply-readall-gets-empty-data-on-finished/23 if(reply->error() == QNetworkReply::NoError) { QByteArray response = reply->readAll(); itemfind->loadPixmap(response); } else { qDebug() << "reply" << reply->error(); } reply->deleteLater(); } } } void ItemFind::loadPixmap(QByteArray data) { QPixmap pixmap; pixmap.loadFromData(data); ui->labelAvantar->setPixmap(pixmap.scaled(60,60)); }