Stack widget pages not properly shown.



  • I have a stackwidget with 5 pages.I also have a comboBox with 5 options corresponding to each page.
    I tried this below code but it was not proper.Plz suggest
    void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
    {
    if(arg1 == "Video")
    {
    ui->AudioPage->hide();
    ui->TickerPage->hide();
    ui->RssPage->hide();
    ui->ImagePage->hide();
    ui->VideoPage->show();
    }
    else if(arg1 == "Image")
    {
    ui->VideoPage->hide();
    ui->AudioPage->hide();
    ui->TickerPage->hide();
    ui->RssPage->hide();
    ui->ImagePage->show();
    }
    else if(arg1 == "RSS")
    {
    ui->VideoPage->hide();
    ui->AudioPage->hide();
    ui->TickerPage->hide();
    ui->RssPage->show();
    ui->ImagePage->hide();
    }
    else if(arg1 == "Ticker")
    {
    ui->VideoPage->hide();
    ui->AudioPage->hide();
    ui->TickerPage->show();
    ui->RssPage->hide();
    ui->ImagePage->hide();
    }
    else if(arg1 == "Audio")
    {
    ui->VideoPage->hide();
    ui->AudioPage->show();
    ui->TickerPage->hide();
    ui->RssPage->hide();
    ui->ImagePage->hide();
    }
    }



  • @kishore_hemmady
    usually you controle the stackedwidget and the stackedwidget controlles the pages

    assuming your stackedwidget has the ObjectName stackedWidget you can write the following:

    void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
    {
    if(arg1 == "Video")
    {
        ui->stackedWidget->setCurrentWidget(ui->VideoPage);
    }
    ...
    ...
    
    if(arg1 == "Audio")
    {
       ui->stackedWidget->setCurrentWidget(ui->AudioPage);
    
    }
    }
    

  • Lifetime Qt Champion

    Hi,

    An even simpler solution would be to match the combo box index with the QStackedWidget content. Then you can connect them directly.

    connect(ui->comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), ui->stackedWidget, &QStackedWidget::setCurrentIndex);
    

    Way less code and it scales automatically when adding new widgets.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.