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

how to switch over to page of staked widget by clicking the combobox text



  • I Tried With push button , Its works Good

    void MainWindow::pushbutton1 clicked()
    {
        ui->stackedWidget->setCurrentIndex(1);
    
    }
    
    void MainWindow::pushbutton2clicked()
    {
        ui->stackedWidget->setCurrentIndex(2);
    
    }
    

    But ,in combo box i dont know how to set the text and get over to page of stack widget

    i have tried some thing like this but its not works , Getting Error

    void MainWindow::on_comboBox_currentIndexChanged(const QString line1  &arg1)
    {
        ui->stackedWidget->setCurrentIndex(1);
    
    }
    void MainWindow::on_comboBox_currentIndexChanged(const QString line2  &arg1)
    {
        ui->stackedWidget->setCurrentIndex(2);
    
    }
    void MainWindow::on_comboBox_currentIndexChanged(const QString line 3  &arg1)
    {
        ui->stackedWidget->setCurrentIndex(3);
    
    }
    
    

    Thanks in advance


  • Moderators

    @sankarapandiyan
    I think, you're working with the assumption, that the argument name has any meaning at all, which it doesn't!

    Take a look at the docu:
    https://doc.qt.io/qt-5/qcombobox.html#currentIndexChanged

    QComboBox has a currentIndexChanged signal, that has 2 overloads, one as string, one as int

    use the int one and do not rely on the connect by name feature, that's bad practice

    void MainWindow::pageSelectionChanged(int index)
    {
          ui->stackedWidget->setCurrentIndex(index);
    }
    
    .....
    connect(ui->comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::pageSelectionChanged);
    


  • @sankarapandiyan
    You are showing 3 identical functions named on_comboBox_currentIndexChanged with the same argument numbers & types. That won't compile. The example you say "works" won't compile either, so it can't work. It helps if you only paste working code.

    Correct the code for push button you show so that it actually compiles and works. Then apply exactly the same principle for naming the 3 on_comboBox_currentIndexChanged methods.


  • Moderators

    @sankarapandiyan
    I think, you're working with the assumption, that the argument name has any meaning at all, which it doesn't!

    Take a look at the docu:
    https://doc.qt.io/qt-5/qcombobox.html#currentIndexChanged

    QComboBox has a currentIndexChanged signal, that has 2 overloads, one as string, one as int

    use the int one and do not rely on the connect by name feature, that's bad practice

    void MainWindow::pageSelectionChanged(int index)
    {
          ui->stackedWidget->setCurrentIndex(index);
    }
    
    .....
    connect(ui->comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MainWindow::pageSelectionChanged);
    

  • Banned

    This post is deleted!

Log in to reply