c++ – QCombobox update results in recursion?

Question:

In general, I want to make it so that when the combobox is opened, it is also updated – updating the list of COM ports in the system.

void MainWindow::updateCOMlist() 
{
    ui->comboBoxCOMport->clear();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        ui->comboBoxCOMport->addItem(info.portName());
    }

}

I do it on a signal:

 connect(ui->comboBoxCOMport,
         SIGNAL(currentIndexChanged(int)),
         this,
         SLOT(updateCOMlist()));

and as I understand it, this action takes place: first, the cleansing occurs, then the first element is added, which causes the slot and the deletion occurs again, and so on.

How can this problem be corrected?

Answer:

Try:

void MainWindow::updateCOMlist() {
    ui->comboBoxCOMport->blockSignals(true);

    ui->comboBoxCOMport->clear();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        ui->comboBoxCOMport->addItem(info.portName());
    }

    ui->comboBoxCOMport->blockSignals(false);
}

PS.

I would understand the architecture, perhaps it can be done differently, but for your example

Scroll to Top