gpt4 book ai didi

qt - 在选项卡式查看模式下,如何在QMdiArea的选项卡旁边添加“新选项卡”按钮?

转载 作者:行者123 更新时间:2023-12-03 15:20:27 26 4
gpt4 key购买 nike

我想有一个“新标签”按钮,就像我的QMdiArea的Chrome或Firefox一样。

我可以在某个按钮或菜单项中添加一个新的子文档到MDI事物中,但是如何使它成为带有“ +”标签的视觉上吸引人的小标签?或者,我会对带有这样的按钮的QTabWidget感到满意。

最佳答案

您将必须为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

关于qt - 在选项卡式查看模式下,如何在QMdiArea的选项卡旁边添加“新选项卡”按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19975137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com