QListWidget items swapping
Hi,
I am trying to swap the content of a QListWidget. Perhaps I am not using the right methods ... or maybe I need to use the listview + model instead ?
The context here is that I have a plotter and I am loading multple files. Each files has its own tab with the plotted data. Each tab contains an object with the filename, channelList, etc ..
The "Channel list" is in a general QListWidget and I would like to switch the content of that widget when I switch tab.
Each dataLog object contains the list of channels with a QList that contains items property:
Code:
QList<QListWidgetItem*> itemList;
And here's the handler when I change tabs :
Code:
void MainWindow::on_tabWidget_logs_currentChanged(int index)
{
foreach(logData* l, this->openedLogs)
{
if(l->parentWidget == ui->tabWidget_logs->widget(index))
{
int count = ui->listWidget_channels->count();
//currentLog->itemList.clear(); // This actually deletes the items in the dataLog object so it's not good
// This doesnt seem to be working since all the items are still in the QListWidget
for ( int i=0; i<count; i++)
{
ui->listWidget_channels->takeItem(i);
}
currentLog = l;
// Take the items from the log object and populate the QListWidget with them
{
ui->listWidget_channels->addItem(i);
}
ui->listWidget_channels->sortItems();
break;
}
}
}
Re: QListWidget items swapping
The problem with your code to remove the items from listWidget_channels is that as soon as you take one item out, "count" is no longer valid. Try this:
Code:
while ( ui->listWidget_channels->count() > 0 )
ui->listWidget_channels->takeItem( 0 );
Re: QListWidget items swapping
Geez ! I really missed that one .. thanks.
I was aware of one bad thing and it always catch me ... the decreasing for loop when the count was in the if statement :
Code:
for (int i=0; i< ui->listWidget_channels->count(); i++ ){ ...
But indeed, having a variable with count before taking items is still wrong.
Thanks again !