Solve the problem of Qt project QCombobox emptying and crashing

Solve the problem of QCombobox emptying and crashing

statement of problem

A QCombobox control is added to the QT interface. According to requirements, when some parameters are modified, the content of QCombobox needs to be reset. But the program crashes when the clear() function is executed.

The source of the problem

Replace the clear() function with:

int nn = ui.comboBoxListProj->count();
for (int i =nn-1;i>-1; i--)
{
    
    
	ui.comboBoxListProj->removeItem(i);
}

Tracking found that an error occurred when deleting item 0. The current item of QCombobox is 0 items. Therefore, it can be inferred that the current selection of QCombobox is deleted.

problem solved

Find the function that QCombobox chooses to change and call, and add fault tolerance judgment:

if (ui.comboBoxListProj->count() < 1) return;
if (isel < ui.comboBoxListProj->currentIndex()) return;

problem solved

to sum up

When QCombobox calls clear() or removeItem(int), it will trigger the QCombobox selection change message because of deleting the current option. At this time, the program will be abnormal because the data is not organized. The solution is: 1. Add fault tolerance judgment, 2. Remove the connection between the message and the slot function, and 3 suspend the control update.

Guess you like

Origin blog.csdn.net/imz2y/article/details/107633867