我有一些代码可以检测到哪个选项卡被选中 QListWidget
int currentTab=ui->tabWidget->currentIndex();
if (currentTab==0)
{
// Code here
}
else if (currentTab==1)
{
// Code here
}
else if (currentTab==2)
{
// code here
}
else if (currentTab==3)
{
// code here
}
Run Code Online (Sandbox Code Playgroud)
如何使用枚举代替if(currentTab==0)或if(currentTab==1)或if(currentTab==2)或if(currentTab==3)
我将以下列方式处理相同的事情(使用枚举类型):
enum Tabs {
Tab1,
Tab2,
Tab3
};
void foo()
{
int currentTab = ui->tabWidget->currentIndex();
switch (currentTab) {
case Tab1:
// Handle the case
break;
case Tab2:
// Handle the case
break;
case Tab3:
// Handle the case
break;
default:
// Handle all the rest cases.
break;
}
}
Run Code Online (Sandbox Code Playgroud)