Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QComboBox setCurrentIndex() crash upon calling



  • Hi,

    I have two panels and its parent is the main window. At any one time only one of the panel is shown and the other panel will be hidden.

    When any one of the panel is shown, some of the QComboBox have certain default values and I have used setCurrentIndex() to set to the default index. However, when the program is executed, upon executing setCurrentIndex() the program crashed.

    MainWindow.cpp

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
       ui->setupUi(this);
    
       panelA = new PanelA(ui->widget);
       panelB = new PanelB(ui->widget);
    
       QObject::connect(this, SIGNAL(resetValuesSignal()), panelA, 
      SLOT(resetValuesSlot()),Qt::AutoConnection);
    
    }
    
    void MainWindow::on_showAPushButton_clicked()
    { 
       panelA->show();
       panelB->hide();
    
       emit resetValues();
    }
    
    

    PanelA.cpp

    PanelA::PanelA(QWidget *parent) : QWidget(parent), ui(new Ui::PanelA)
    {
       ui->setupUi(this);
    
      QObject::connect(ui->combobox,SIGNAL(currentIndexChanged(int)), 
      this,SLOT(showWidgetSlot(int)), Qt::AutoConnection);
    }
    
    void PanelA::resetValuesSlot()
    {
        ui->combobox->setCurrentIndex(1); // crash here
    }
    

    Please advise. Thank you.


  • Lifetime Qt Champion

    @qt-1234 said in QComboBox setCurrentIndex() crash upon calling:

    ui->combobox->setCurrentIndex(1);

    How many elements do you have there? If only one then 1 would be out of range.
    Is ui->combobox a valid pointer?

    Also, what kind of crash is it?
    Did you debug?
    Do you have a stack trace after crash?


Log in to reply