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 ?

    alt text

    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));
    }
    

  • Moderators

    @sonichy

    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));
    }

  • Moderators

    @sonichy

    Glad to read that you have solved your issue. Thanks for providing a potential solution for others.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.