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

后端 未结 5 1181
天命终不由人
天命终不由人 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:20

    Similar concept to @Garjy's answer:

    You could use a "blank" tab and add a button to that tab. This will also replace the "close" button if you are using TabWidget.setTabsCloseable(True). It is possible to make it to the "blank" tab, so I suggest combining with @Garjy's answer or adding some text/ another new button.

    import sys
    from qtpy.QtWidgets import QTabWidget, QWidget, QToolButton, QTabBar, QApplication
    
    class Trace_Tabs(QTabWidget):
    
        def __init__(self):
            QTabWidget.__init__(self)
            self.setTabsClosable(True)
            self._build_tabs()
    
        def _build_tabs(self):
    
            self.insertTab(0, QWidget(), "Trace 0" )
    
            # create the "new tab" tab with button
            self.insertTab(1, QWidget(),'')
            nb = self.new_btn = QToolButton()
            nb.setText('+') # you could set an icon instead of text
            nb.setAutoRaise(True)
            nb.clicked.connect(self.new_tab)
            self.tabBar().setTabButton(1, QTabBar.RightSide, nb)
    
        def new_tab(self):
            index = self.count() - 1
            self.insertTab(index, QWidget(), "Trace %d" % index)
            self.setCurrentIndex(index)
    
    if __name__ == '__main__':
    
        app = QApplication(sys.argv)
    
        tabs = Trace_Tabs()
        tabs.show()
    
        app.exec_()
    

提交回复
热议问题