Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QComboBox setCurrentIndex() crash upon calling
Forum Updated to NodeBB v4.3 + New Features

QComboBox setCurrentIndex() crash upon calling

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 489 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qt.1234
    wrote on last edited by qt.1234
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • Q qt.1234

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved