QList<QPixmap> 'index out of range'



  • I'm trying to make a list of pixmaps for a program I'm writing, but when I try to get something from the list, I get this error:

    ASSERT failure in QList<T>::at: "index out of range", file ........\QtSDK\Desktop\Qt\4.7.3\mingw\include/QtCore/qlist.h, line 456
    Invalid parameter passed to C runtime function.
    Invalid parameter passed to C runtime function.

    When I went into Qt Creator's debug mode, I found that the list of pixmaps wasn't being populated at all. I think I need to create a new QList<Pixmap> in the MainWindow constructor (with 'thumbnailPixmapList = new QList<QPixmap>();'), but the compiler gives me this error:

    error: no match for 'operator=' in '((MainWindow*)this)->MainWindow::thumbnailPixmapList = (operator new(4u), (<statement>, ((QList<QPixmap>*)<anonymous>)))'

    This is the function I'm using to populate the list:
    @void MainWindow::createThumbnailPixmaps()
    {
    for (int i=0; i<thumbnailPixmapList.size(); ++i)
    {
    QPixmap thumbnailPix(QPixmap::fromImage(extractSlice(0,i)));
    thumbnailPixmapList.insert(i, thumbnailPix);
    }
    }@

    This is one of the functions I want to use to get a list item:
    @void MainWindow::forwardSlice()
    {
    QPixmap nextPix = thumbnailPixmapList.at(s+1);
    s+=1;
    ui->horizontalSlider->setValue(s);
    newSlice2->setPixmap(nextPix);
    ui->sliceView->update();
    }@

    Please let me know if there's anything I'm doing wrong.


  • Moderators

    [quote author="jesbb" date="1309186814"]
    @void MainWindow::forwardSlice()
    {
    QPixmap nextPix = thumbnailPixmapList.at(s+1);
    s+=1;
    ui->horizontalSlider->setValue(s);
    newSlice2->setPixmap(nextPix);
    ui->sliceView->update();
    }@

    [/quote]

    That part does not look safe at all.
    What is the contents of "s" ? You might want to make sure that it is within the bounds 0 <= s < thumbnailPixmapList.size().


  • Moderators

    [quote author="jesbb" date="1309186814"]
    @void MainWindow::createThumbnailPixmaps()
    {
    for (int i=0; i<thumbnailPixmapList.size(); ++i)
    {
    QPixmap thumbnailPix(QPixmap::fromImage(extractSlice(0,i)));
    thumbnailPixmapList.insert(i, thumbnailPix);
    }
    }@
    [/quote]

    Was a little fast in answering. Here is another problem. How do you want to loop if nothing is in the list?



  • 's' is an integer; its default value is 50. If I wanted to make sure if 0 <= s < thumbnailPixmapList.size(), would I need to make an if loop to put inside the forwardSlice method?

    I haven't thought about how I would loop if nothing is in the list.


  • Moderators

    I do not know where you are getting the parts of your picture, but all looks a little suspicious to me.

    Assuming that somehow extractSlice (0, j) makes sense for j up to 9. This would make more sense.

    @void MainWindow::createThumbnailPixmaps()
    {
    for (int j=0; j < 10; ++j)
    {
    QPixmap thumbnailPix(QPixmap::fromImage(extractSlice(0,j)));
    thumbnailPixmapList.push_back (thumbnailPix);
    }
    }@

    For reading you could use something similar to:
    @void MainWindow::forwardSlice()
    {
    if ( s >= 0 && s < thumbnailPixmapList.size() )
    {
    QPixmap nextPix = thumbnailPixmapList.at(s);
    ++s;
    ui->horizontalSlider->setValue(s);
    newSlice2->setPixmap(nextPix);
    ui->sliceView->update();
    }
    }@

    Disclaimer: This part of code is typed directly into the thread. Furthermore, I cannot know what you are up to. So, the snippet might need significant additions until it runs as you need.



  • I made some minor changes to the code you gave me and it works now.

    Thank you very much for your help; I really appreciate it!


  • Moderators

    You
    are
    welcome :-)



  • To see what triggers this type of error you should run it in qt-creator in the debugger then click on each higher function in the call stack and inspect the local variables to see what causes the problem.


Log in to reply
 

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