Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

For pair loop and qlistwidget



  • Hello guys. I can't resolve my problem.

    I have a QListWidget with an item using QMultiMap. When I add a new item he is created successfully, but if I create one more it is not showing on.
    ss1.png
    ss2.png

    Like u see in picture 2 item is adding to QMultiMap, but my loop is not showing him. I resolved it by changing my loop into this:

        ui->dailyTasksWidget->clear();
    
    
        for(auto key: DailyTaskMap.keys())
        {
                 if(key == nametodisplay)
                  {
                      //ui->dailyTasksWidget->clear();
    
                      for(auto task : DailyTaskMap.values())
                        {
                          ui->dailyTasksWidget->addItem(task);
                        }
                  }
        }
    

    but now I have double the same items.
    ss3.png

    How I can fix it?


  • Lifetime Qt Champion

    @naax said in For pair loop and qlistwidget:

    ui->dailyTasksWidget->clear();

    That's the line that remove everything hence my question.


  • Lifetime Qt Champion

    Hi,

    How are you managing that multi map ?
    Are you sure your for loop uses the latest version of it ?



  • @SGaist

    Yes. I added one more line

     ui->dailyTasksWidget->clear();
    

    before the second loop and it works, but the problem is with deleting current items now. Sorry that I'm mixing everything, but adding items and deleting current items are creating problems with one another.

    void MainWindow::highlightChecked(QListWidgetItem *item)
    {
         if(item->checkState() == Qt::Checked)
        {
    
              for(auto& task: DailyTaskMap.values(nametodisplay))
              {
                      Euro+=100.5;
                      UpdateData();
    
                      nametask = ui->dailyTasksWidget->currentItem()->text();
                      DailyTaskMap.remove(nametodisplay,nametask);
    
                       delete ui->dailyTasksWidget->currentItem();
                      qDebug() << DailyTaskMap.keys();
                      qDebug() << DailyTaskMap.values();
    
                      SaveDailyTasks();
                           }
        }
           else
        {
    
        }
    
    

    So... I can create items successfully now, but if I want to delete one of the items, then I have a problem because all items are deleting.
    ss1.png
    ss2.png


  • Lifetime Qt Champion

    Why are you deleting all items ?



  • @SGaist

    I don't want to delete all items. Only this one, selected, but I have a bug in my code and I don't know where is the problem.

                      delete ui->dailyTasksWidget->currentItem(); //delete current item, but this delete all items, why?
    

    then delete the item from the map

    nametask = ui->dailyTasksWidget->currentItem()->text();
    DailyTaskMap.remove(nametodisplay,nametask); //nametodisplay this is special key for every day
    

  • Lifetime Qt Champion

    @naax said in For pair loop and qlistwidget:

    ui->dailyTasksWidget->clear();

    That's the line that remove everything hence my question.



  • @SGaist

    
              for(auto& task: DailyTaskMap.values(nametodisplay))
              
    

    this loop was a problem. I got this. Thanks a lot anyway!


Log in to reply