Qt World Summit: Register Today!

[SOLVED]Multiple requests using QNetworkAccessManager

  • Hello!

    I need to send 5 requests to a site which replies for me a image file. I am using the reply to add the image to a pixmap, then adding it to a new QList<QLabel *> item. The problem is that if I use any kind of loop it blocks (for loop from 0 to 4, while(timer.isActive()). I think I managed to send the requests using a recursive way (call a function in the constructor of the class, I have a slot for reply->finished(), and in the slot I call the function again with the new parameters) but now it locks when I add the QLabels from my list to the windows I want to display... Don't know if it blocks because of the requests or the function for adding the layout... Can anyone help?

    Class inherits QWidget.

    @void Class::addLayout() {
    QVBoxLayout *lay = new QVBoxLayout();
    for(int i = 0; i < list.size(); i++) {


  • I don't think it's blocks because of requests:
    "Here":http://qt-project.org/doc/qt-4.8/QNetworkAccessManager.html#details saids:

    bq. QNetworkAccessManager queues the requests it receives. The number of requests executed in parallel is dependent on the protocol. Currently, for the HTTP protocol on desktop platforms, 6 requests are executed in parallel for one host/port combination.

    I've tried to run your method addLayout and it works correctly only once, the second call generate warning:
    @QWidget::setLayout: Attempting to set QLayout "" on Class "", which already has a layout@
    and nothing happens. That means that new widgets should be added for alredy existed layout (by this->layout() method), instead of creating new.

  • Well... I call the add layout after all the list is loaded (it says it creates a list of labels with size 11, 5 images and 6 with text, which is exactly what I need), thus I create the new layout only once... I think I'm loading the pixmaps wrong... Because when I just try to show a image label my program crashes... Tried with just one network request and it loads the pixmap ok. I am using something like:

    @ QLabel *label = new QLabel();
    QPixmap *pix = new QPixmap();
    list << label;
    delete pix;
    delete label; // so I can create new ones for each request@

    As an alternative solution, I'm trying to load the images in a QTextBrowser (because I need them clickable) but I can't seem to get the html tag to load my image from the URL.

    @textBrowser->insertHtml("<img src="url">@

    and it gives me an error:
    @QFSFileEngine::open: No file name specified@

  • @list << label;
    delete pix;
    delete label; // so I can create new ones for each request@

    Note that list doesn't create a copy of label, it just stores pointer to it. So, when you delete label, your pointer in list becomes broken.

  • removing the deletes worked. thank you :)

Log in to reply