if else to switch case for GUI



  • hi my freinds i working on image processing project , works fine ... but i want to move to SWITH CASE insread of IF ELSE ...

    if (ui->medianbur->isChecked())
    { // program works fine ...
    }

    is this correct :
    switch () // no argument yet ...
    case : (ui->medianbur->isChecked())
    { // put the same program here....
    }


  • Moderators

    @Amine-Djeddi The syntax of your switch is wrong, but this is completely unrelated to Qt.
    One more thing: switch does not make sense when using with binary conditions. isChecked() returns a boolean, so you will have exactly 2 possibilities. Why do you want to use switch here? switch makes sense if you have many posibilities to check.



  • yes i have more than 2 possibilites like :

    if (ui->medianbur->isChecked())
    { // program works fine ...
    }
    if (ui->Grayscale->isChecked())
    { // program works fine ...
    }

    if (ui->cannymode->isChecked())
    { // program works fine ...
    }
    i want to convert this into switch case ,

    im blocked here :

    switch (ui->medianblur->isChecked())
    case true :
    {

    	// programme work fine 
    }


  • @Amine-Djeddi
    As @jsulm stated, it is pointless/unhelpful to use a switch against a condition which is boolean (values true or false). Use an if, possibly with an else. If necessary you can use:

    if (...)
        ...
    else if (...)
        ...
    else if (...)
        ...
    else
        ...
    

    Or, you can combine your conditions with && and ||s.

    There is nothing about what you have shown which would benefit from a switch().



  • ok i got it , thank you @jsulm and @JonB for make things clear


Log in to reply
 

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