如何在选项卡式视图模式下在QMdiArea的选项卡旁边添加"新选项卡"按钮?

rub*_*nvb 15 qt tabs qtabwidget qmdiarea

我希望有一个"新标签"按钮,就像Chrome或Firefox一样QMdiArea.

我可以在某处创建一个按钮或菜单项,为MDI添加一个新的子文档,但是如何使它成为一个带有"+"标签的视觉上吸引人的小标签?或者,我会很满意QTabWidget这样一个按钮.

tro*_*ane 15

我知道这个问题已经过时了,但前段时间我一直在寻找您所要求的功能的即用型实现.我挖了一下并为Qt 5实现了这个 - 看一下repo.

主要想法是:

// Create button what must be placed in tabs row
QToolButton *tb = new QToolButton();
tb->setText("+");
// Add empty, not enabled tab to tabWidget
tabWidget->addTab(new QLabel("Add tabs by pressing \"+\""), QString());
tabWidget->setTabEnabled(0, false);
// Add tab button to current tab. Button will be enabled, but tab -- not
tabWidget->tabBar()->setTabButton(0, QTabBar::RightSide, tb);
Run Code Online (Sandbox Code Playgroud)

  • 这在 Qt 5 中效果很好,但对于 Qt 4 这给了我一个错误:`error: 'QTabBar* QTabWidget::tabBar() const' is protected`,所以我想我只需要为此忘记 Qt 4然后!虽然简单又优雅。原则上我可以使用 QLabel 的任何图像,这是一个很好的优点。 (2认同)
  • 我检查了一下,发现 `tabBar` 在 Qt 4.8 中确实受到保护。所以我在我的答案中添加了“Qt 5”。谢谢你的评论。 (2认同)
  • 如果选项卡是可移动的(setMovable(真)),则可能是"新标签"特别棒后拖和拖放标签,即使它被禁用:(. (2认同)

jus*_*gel 9

你必须为QTabBar编写自己的类.可以使用绝对定位添加加号按钮.

我这里有一些代码用于PySide; 它应该给你基本的想法.

class TabBarPlus(QtGui.QTabBar):
    """Tab bar that has a plus button floating to the right of the tabs."""

    plusClicked = QtCore.Signal()

    def __init__(self):
        super().__init__()

        # Plus Button
        self.plusButton = QtGui.QPushButton("+")
        self.plusButton.setParent(self)
        self.plusButton.setFixedSize(20, 20)  # Small Fixed size
        self.plusButton.clicked.connect(self.plusClicked.emit)
        self.movePlusButton() # Move to the correct location
    # end Constructor

    def sizeHint(self):
        """Return the size of the TabBar with increased width for the plus button."""
        sizeHint = QtGui.QTabBar.sizeHint(self) 
        width = sizeHint.width()
        height = sizeHint.height()
        return QtCore.QSize(width+25, height)
    # end tabSizeHint

    def resizeEvent(self, event):
        """Resize the widget and make sure the plus button is in the correct location."""
        super().resizeEvent(event)

        self.movePlusButton()
    # end resizeEvent

    def tabLayoutChange(self):
        """This virtual handler is called whenever the tab layout changes.
        If anything changes make sure the plus button is in the correct location.
        """
        super().tabLayoutChange()

        self.movePlusButton()
    # end tabLayoutChange

    def movePlusButton(self):
        """Move the plus button to the correct location."""
        # Find the width of all of the tabs
        size = sum([self.tabRect(i).width() for i in range(self.count())])
        # size = 0
        # for i in range(self.count()):
        #     size += self.tabRect(i).width()

        # Set the plus button location in a visible area
        h = self.geometry().top()
        w = self.width()
        if size > w: # Show just to the left of the scroll buttons
            self.plusButton.move(w-54, h)
        else:
            self.plusButton.move(size, h)
    # end movePlusButton
# end class MyClass

class CustomTabWidget(QtGui.QTabWidget):
    """Tab Widget that that can have new tabs easily added to it."""

    def __init__(self):
        super().__init__()

        # Tab Bar
        self.tab = TabBarPlus()
        self.setTabBar(self.tab)

        # Properties
        self.setMovable(True)
        self.setTabsClosable(True)

        # Signals
        self.tab.plusClicked.connect(self.addTab)
        self.tab.tabMoved.connect(self.moveTab)
        self.tabCloseRequested.connect(self.removeTab)
    # end Constructor
# end class CustomTabWidget
Run Code Online (Sandbox Code Playgroud)


Gar*_*rjy 7

为什么不在QTabWidget的最后一个标签中创建一个按钮?只需创建最后一个带有"+"符号的选项卡,然后使用currentChanged事件.

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_()
Run Code Online (Sandbox Code Playgroud)

  • 然后使用以下方法解决该问题:self.tabBar().setSelectionBehaviorOnRemove(QTabBar.SelectLeftTab) (3认同)