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

What's the best way to change the UI based on the QComboBox index?



  • So basically I need that my UI adapts to the index of the QCombomBox, and I done it creating all the widgets in the QtCreator and hiding or showing it depending on the index of the QComboBox, this is my code:

    void MainWindow::on_c_tipo_currentIndexChanged(int index)
    {
        if(index == 0){
            ui->s_numcomp->setHidden(false);
            ui->label_numcomp->setHidden(false);
            ui->t_rmv_teste->setHidden(false);
            ui->t_adc_teste->setHidden(false);
            ui->t_resposta_prog->setHidden(false);
            ui->t_resposta_mult->setHidden(true);
            ui->t_resposta_discursiva->setHidden(true);
            ui->t_adc_questao->setHidden(true);
            ui->t_rmv_questao->setHidden(true);
        }
        else if(index == 1){
            ui->s_numcomp->setHidden(true);
            ui->label_numcomp->setHidden(true);
            ui->t_rmv_teste->setHidden(true);
            ui->t_adc_teste->setHidden(true);
            ui->t_resposta_mult->setHidden(false);
            ui->t_resposta_discursiva->setHidden(true);
            ui->t_resposta_prog->setHidden(true);
            ui->t_adc_questao->setHidden(false);
            ui->t_rmv_questao->setHidden(false);
        }
        else{
            ui->s_numcomp->setHidden(true);
            ui->label_numcomp->setHidden(true);
            ui->t_rmv_teste->setHidden(true);
            ui->t_adc_teste->setHidden(true);
            ui->t_resposta_mult->setHidden(true);
            ui->t_resposta_discursiva->setHidden(false);
            ui->t_resposta_prog->setHidden(true);
            ui->t_adc_questao->setHidden(true);
            ui->t_rmv_questao->setHidden(true);
        }
    }
    

    Is there a better way to do that? I'm new to Qt and this for me looks like i'm doing something wrong. Here's some pictures of the setup in the QtCreator and my program running.
    creator.png
    discursiva.png multiplaescolha.png


  • Qt Champions 2019


  • Lifetime Qt Champion

    Hi
    Welcome to the forum.
    As @jsulm says, StackedWidget is perfect if the combox flips between sort of pages.

    If that dont fit completely your use case, let us know as there are many ways to
    control the widget besides manually hide /show :)



  • @jsulm Thanks, I will look into it and try to put in my project!



  • @mrjj I looked and I think is fits perfectly, thanks for the reply!


Log in to reply