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. QListWidget->clear() throws std::bad_alloc
Forum Updated to NodeBB v4.3 + New Features

QListWidget->clear() throws std::bad_alloc

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.4k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    RBrNx277
    wrote on last edited by
    #1

    I am currently using a QListWidget in my main window, which displays a list of all the levels my program generates (Level 1, Level 2, etc). If I run my program as normal and keep pressing the "Generate" button without doing anything else then my program will work perfectly fine, it will keep generating levels and updating the QListWidget as it should.

    However, If I Select one of the items in the QListWidget by clicking on it, and THEN hit the "Generate" button, my program will crash and throw a bad_alloc. After doing some debugging I found that it throws the bad_alloc when trying to execute QListWidget->clear(). I have absolutely no idea why this would be happening, has anyone seen this before?

    void MainWindow::on_generateButton_released()
    {
        Generator.clearVectors();
        ui->list_LevelSet->clear(); //std::bad_alloc here
        Generator.generateLevel();
    }
    

    Thanks!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Does your QListWidget contain coming from Generator ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RBrNx277
        wrote on last edited by
        #3

        Hi there, to add items to my QListWidget I have a SIGNAL in my Generator class and a SLOT in my MainWindow Class.
        The Slot function looks like this:

        void MainWindow::addToList(int value){
            ui->list_LevelSet->addItem("Level " + QString::number(value));
            QVariant dataValue(value-1);
            ui->list_LevelSet->item(value-1)->setData(Qt::UserRole, dataValue);
        }
        

        So the QListWidget contains x number of items which have the text "Level x" and each item stores the level number (x) as data.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RBrNx277
          wrote on last edited by
          #4

          EDIT: Please ignore this post, I've figured out the error. I was originally connecting my QListWidget to a QGraphicsView with the follow connect function

          connect(ui->list_LevelSet, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(displayLevel(QListWidgetItem*)));
          

          However I ended up changing the SIGNAL (and subsequently the displayLevel function) to use

          connect(ui->list_LevelSet, SIGNAL(currentRowChanged(int)), this, SLOT(displayLevel(int)));
          

          so that the QGraphicsView would be changed whenever the List was updated, instead of making the user click on an item to update the QGrapicsView. This meant that when I called Clear(); it would of course change the row, which would in turn attempt to display a level that didn't exist and therefore throw the bad_alloc.

          My new function looks like this:

          void MainWindow::on_generateButton_released()
          {
              display = false;
              Generator.clearVectors();
              ui->list_LevelSet->clear();
              display = true;
              Generator.generateLevel();
          }
          

          and I check if 'display' is set to true before display the level.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            IIRC, you can use the blockSignals method to achieve the same effect without the need for an additional flag variable.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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