artoon
artoon

Reputation: 739

Qt 5 QComboBox setCurrentIndex

I use :

ui->Combobox->setCurrentIndex(1);

but this instruction doesn't call the function

void on_comboBox_currentIndexChanged(const QString &arg1);

Why this behaviour ?

void ConsigneMouvement::show(int AxeType) 
{
    axeType = AxeType;
    switch(axeType)
    {
        case 1:
            ui->comboBox->setCurrentIndex(0);
            ui->comboBox->setEnabled(true);
        break;
        case 2 :
            ui->comboBox->setCurrentIndex(1);
            ui->comboBox->setEnabled(false);
        break;
        case 3 :
            ui->comboBox->setCurrentIndex(0);
            ui->comboBox->setEnabled(true);
        break;
        case 4 :
            ui->comboBox->setCurrentIndex(0);
            ui->comboBox->setEnabled(true);
        break;
    }
    this->exec();  
}

and the function

void ConsigneMouvement::on_comboBox_currentIndexChanged(const QString &arg1) 
{
    if(arg1 == "Absolu")
        ui->label_distance->setText(tr("Position"));
    else
        ui->label_distance->setText(tr("Distance")); 
}

Upvotes: 0

Views: 4990

Answers (2)

Slavenskij
Slavenskij

Reputation: 649

setCurrentIndex() does not emit currentIndexChanged() signal if it is not really changed. It means that in your example the previous index was already 1.

Upvotes: 1

Zlatomir
Zlatomir

Reputation: 7034

Most likely the slot isn't connected automatically because you wrote comboBox in one place and Combobox in the other, so try:

void on_Combobox_currentIndexChanged(const QString &arg1);

//you'll need to run qmake before build

LE: i recommend to avoid the auto-connect feature and write the connect statements yourself (you need to be careful to avoid naming slots like on_WIDGETNAME_SIGNALNAME because you might end up with a slot called twice)

Upvotes: 0

Related Questions