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....
} -
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....
}@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 }
-
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 aswitch
against a condition which is boolean (valuestrue
orfalse
). Use anif
, possibly with anelse
. 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()
.