Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QList<QPixmap> 'index out of range'
QtWS25 Last Chance

QList<QPixmap> 'index out of range'

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 6.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jesbb
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      [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().

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        [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?

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jesbb
          wrote on last edited by
          #4

          '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.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            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.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jesbb
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                You
                are
                welcome :-)

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  ZapB
                  wrote on last edited by
                  #8

                  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.

                  Nokia Certified Qt Specialist
                  Interested in hearing about Qt related work

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved