QTreeWidget Delete Last Item



  • hi
    when QTreeWidget just have 1 item & if i want delete the item application crash & when have more than 1 item work fine, this is my code :

    ui->TreeWidget->takeTopLevelItem(currentItem);
    

  • Lifetime Qt Champion

    Hi,

    Do you have a stack trace ?

    Are you sure currentItem is valid ?



  • @SGaist Hi
    No, i don't have stack trace & currentItem is valid even i test whit this code when i have only 1 item:

    ui->TreeWidget->takeTopLevelItem(0);
    

    and app crash



  • Maybe there is some side effect even that happens which is actually the source of the crash. For example, you might have a problem if you don't handle the signal itemSelectionChanged() properly (i.e. you don't expect a no-selection case).

    I tried this without problems:

    	while(d_machine_parameter_tree->topLevelItemCount())
    	{
    		d_machine_parameter_tree->takeTopLevelItem(0);
    	}
    

    Qt 5.6.0, OSX 10.10.5

    Note: You should delete the returned widget otherwise it is a memory leak.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.