[SOLVED] After setting default item index of QCombobox will not be updated on GUI

  • Hi,

    I would like to add items to my combobox during starting up my application. And after adding all my items I would like to change the current index to my default. Sadly on the GUI the combox is blank if my program starts up. If I click on the combobox I will see the added items. Do I have to call some update method?

    What I'm wondering too is if don't call setCurrentIndex() method. Then the combox will not be blank and index 0 is visible.

    Thanks in advance for your help.

    @MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    for(UINT8 i = 0; i < ( sizeof(myList)/sizeof(myList[0]) ); i++)
        ui->comboBox->addItem( myList[i] );
    connect(ui->comboBox, SIGNAL( currentIndexChanged(int) ), this, SLOT( mySlot(int) ) );
    emit ui->comboBox->setCurrentIndex(4);


  • Moderators


    Welcome to Qt forum.
    emit ui->comboBox->setCurrentIndex(4);

    "setCurrentIndex":http://qt-project.org/doc/qt-5/qcombobox.html#currentIndex-prop is a normal function, no need to use emit.

  • I've taken away the emit but sadly combobox stays still empty in initial state.

  • Moderators

    Hmm that is because you are setting currentIndex to that of the seperator. Change any one.

  • It's now running. The methods insertSeperator() and setCurrentIndex() should not have the same index. Solution below.

    ui->comboBox->setCurrentIndex(4 + 1);@

Log in to reply