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.
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.
How I can fix it?
-
@naax said in For pair loop and qlistwidget:
ui->dailyTasksWidget->clear();
That's the line that remove everything hence my question.
-
Hi,
How are you managing that multi map ?
Are you sure your for loop uses the latest version of it ? -
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.
-
Why are you deleting all items ?
-
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
-
@naax said in For pair loop and qlistwidget:
ui->dailyTasksWidget->clear();
That's the line that remove everything hence my question.