How can I add a “new tab” button next to the tabs of a QMdiArea in tabbed view mode?

后端 未结 5 1192
天命终不由人
天命终不由人 2020-12-05 14:46

I\'d like to have a \"new tab\" button much like Chrome or Firefox has for my QMdiArea.

I can make a button or menu item somewhere that adds a new subdo

5条回答
  •  旧巷少年郎
    2020-12-05 15:19

    Why not make a button out of the last tab of your QTabWidget ? Just create the last tab with a '+' symbol on it and use the currentChanged event.

    class Trace_Tabs(QTabWidget):
    
        def __init__(self):
            QTabWidget.__init__(self)       
            self._build_tabs()
    
        def _build_tabs(self):
            self.setUpdatesEnabled(True)
    
            self.insertTab(0,QWidget(), "Trace" )
            self.insertTab(1,QWidget(),'  +  ') 
    
            self.currentChanged.connect(self._add_trace) 
    
        def _add_trace(self, index):    
    
            if index == self.count()-1 :    
                '''last tab was clicked. add tab'''
                self.insertTab(index, QWidget(), "Trace %d" %(index+1)) 
                self.setCurrentIndex(index)
    
    if __name__ == '__main__':    
        app = QApplication([])    
        tabs = Trace_Tabs()
        tabs.show()    
        app.exec_()
    

提交回复
热议问题