I need every QList ImageList; assign a specific index ... To do this, I create a QList <QList <QImage>> SourceImageList; and in the loop I try to add values ​​to this list:

 if (QueryDesk->exec("SELECT desk_id,name_desk FROM tank.desk WHERE ship_id = 3")) { int deskCount = 0; while (QueryDesk->next()) { if (QueryDeckLayout->exec(QString("SELECT url,name FROM tank.deck_layout WHERE desk_id = %1").arg(QueryDesk->value(0).toString()))) { int countImg=0; while (QueryDeckLayout->next()) { SourceImageList.at(deskCount).value(countImg,shemeImg); countImg++; } } } } 

But I get an error

 ASSERT failure in QList<T>::at: "index out of range", file C:\Qt\5.11.0\msvc2017_64\include\QtCore/qlist.h, line 541 Debug Error! 

This is why SourceImageList.at(deskCount).value(countImg,shemeImg); Can't add values ​​to the list and how can this be done? ////////////////////////////////////////////////// //////////////////////////////

  if (QueryDesk->exec("SELECT desk_id,name_desk FROM tank.desk WHERE ship_id = 3")) { int deskCount = 0; while (QueryDesk->next()) { if (QueryDeckLayout->exec(QString("SELECT url,name FROM tank.deck_layout WHERE desk_id = %1").arg(QueryDesk->value(0).toString()))) { int countImg=0; while (QueryDeckLayout->next()) { QImage shemeImg; shemeImg.load(FI.absoluteFilePath()); tempSourceImageMap.insert(countImg,shemeImg); countImg++; } SourceImageMap.insert(tankCount,tempSourceImageMap); } deskCount++; } } 

.... How can I run through the SourceImageMap values ​​now?

 for (int i=0;i<SourceImageMap.take(index).size();i++) { SourceImageMap.take(index).take(i); } 

So SourceImageMap.take(index).size() does not always equal 0

  • 2
    QList :: append () - adds an item to the list, at () - gets an already existing item from the list by its index / position in the list - Alexander Chernin

1 answer 1

You are trying to misuse the container. Lists for that and lists to treat them as lists. In your case, you need an associative array, and for this there is a map - use it and use it for such purposes. True, and there you will not be able to add elements with at, since this method is SPECIALLY created in order to receive EXISTING elements, and if there are none, throw an exception.

  • QMap <int, QMap <int, QImage >> SourceImageMap; How to run through the entire list like this? for (int i = 0; i <SourceImageMap.take (index) .size (); i ++) {SourceImageMap.take (index) .take (i);} This does not work - Ivan Triumphov
  • one
    @IvanTriumphov and for whom iterators? And in your case, the range for is enough - Andrej Levkovitch
  • I redid the question in my SourceImageMap.take (index) .size () is always 0 ..... How can I run through the values ​​of SourceImageMap? - Ivan Triumphov
  • @IvanTriumphov here what is not clear in the word iterator or a range for loop? Finally, learn the basic concepts of c ++ and do not ask stupid questions (and twice). - Andrej Levkovitch
  • this forum is not only for programmer gurus, but also for those who study ...... I figured it out myself - Ivan Triumphov