一杯清酒邀明月
天下本无事,庸人扰之而烦耳。

QT中QTabWidget在Qt 5.15 引入了 setTabVisible方法

低版本实现方法

 1 //TabWidget
 2 class TabWidget : public QTabWidget
 3 {
 4     Q_OBJECT
 5  
 6 public:
 7  
 8     class widgetTab {
 9     public:            
10         QString text;
11         QWidget* widget;
12         bool visable;
13     };
14  
15     TabWidget(QWidget* parent = 0);    
16     ~TabWidget();
17     
18     bool showTab(int index);
19     bool hideTab(int index);
20  
21 protected:
22     virtual void tabInserted(int index);   
23  
24 private:
25     QList<widgetTab> _tabpageWidgets;
26  
27 };
 1 //TabWidget
 2 TabWidget::TabWidget(QWidget* parent)
 3 :QTabWidget(parent)
 4 {
 5  
 6 }
 7     
 8 TabWidget::~TabWidget()
 9 {
10  
11 }
12     
13 void TabWidget::tabInserted(int index)
14 {
15     QWidget *curr = widget(index);
16     
17     int realindex = -1;
18     
19     for(int x = 0; x < _tabpageWidgets.size(); x++)
20     {
21         const widgetTab &tempTab = _tabpageWidgets.at(x);
22         
23         if(tempTab.widget == curr)
24         {
25             realindex = x;
26             break;
27         }
28     }    
29         
30     if(realindex == -1)
31     {      
32   
33         widgetTab tempTab;
34         QString text =this->tabBar()->tabText(index);
35         QIcon icon = this->tabBar()->tabIcon(index);
36         tempTab.text = text;
37         tempTab.widget = curr;
38         tempTab.visable = true;
39         _tabpageWidgets.append(tempTab);
40     }    
41 }
42  
43  
44 bool TabWidget::showTab(int index)
45 {
46     
47     if(index >= 0 && index < _tabpageWidgets.size())
48     {    
49         widgetTab &tempTab = _tabpageWidgets[index];
50             
51         if(tempTab.visable == false)
52         {
53             this->insertTab(index, tempTab.widget, tempTab.text);
54             tempTab.visable = true;
55             return true;
56         }
57     }
58     
59     return false;
60 }
61  
62 bool TabWidget::hideTab(int index)
63 {
64     if(index >= 0 && index < _tabpageWidgets.size())
65     {
66         widgetTab &tempTab = _tabpageWidgets[index];
67         
68         int idx = indexOf(tempTab.widget);
69         
70         if(idx >= 0)
71         {
72             this->removeTab(idx);
73             tempTab.visable = false;
74             return true;
75         }
76     }
77     
78     return false;
79 }

 

posted on 2024-02-19 19:36  一杯清酒邀明月  阅读(1128)  评论(0编辑  收藏  举报