Qt Designer 使小部件在布局中与另一个小部件重叠
Qt Designer make widget overlap another widget in layout
我想用Qt Designer和PyQt制作3个window像上图一样的按钮(类似GoogleChrome)。
我想要 3 个按钮重叠在 TabWidget 的右侧。
但是我只能在像图片那样打破布局时重叠TabWidget上的Button。
当我设置任何布局时,每个小部件都不能相互重叠。
那么我可以在设置布局时重叠吗?谢谢。
这是我想要的布局
它类似于Google Chrome的布局
这无法在 creator/designer 中完成,只能使用您的代码中的 setCornerWidget()
来实现。
由于每个角落只能设置一个小部件,因此您必须创建一个充当容器的QWidget,然后将按钮添加到其中。
class Test(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# ...
self.createButtons()
def createButtons(self):
# create the container and its layout
self.buttonContainer = QtWidgets.QWidget()
buttonLayout = QtWidgets.QHBoxLayout(self.buttonContainer)
# remove margins around the layout and set a minimal spacing between
# the children widgets
buttonLayout.setContentsMargins(0, 0, 0, 0)
buttonLayout.setSpacing(1)
# QToolButtons are usually better for this, as QPushButtons tend
# to expand themselves
self.minimizeButton = QtWidgets.QToolButton(text='_')
self.maximizeButton = QtWidgets.QToolButton(text='o')
self.closeButton = QtWidgets.QToolButton(text='x')
buttonLayout.addWidget(self.minimizeButton)
buttonLayout.addWidget(self.maximizeButton)
buttonLayout.addWidget(self.closeButton)
# set the container as the corner widget; as the docs explain,
# despite using "TopRightCorner", only the horizontal element (right
# in this case) will be used
self.tabWidget.setCornerWidget(
self.buttonContainer, QtCore.Qt.TopRightCorner)
我想用Qt Designer和PyQt制作3个window像上图一样的按钮(类似GoogleChrome)。
我想要 3 个按钮重叠在 TabWidget 的右侧。 但是我只能在像图片那样打破布局时重叠TabWidget上的Button。
当我设置任何布局时,每个小部件都不能相互重叠。 那么我可以在设置布局时重叠吗?谢谢。
这是我想要的布局
它类似于Google Chrome的布局
这无法在 creator/designer 中完成,只能使用您的代码中的 setCornerWidget()
来实现。
由于每个角落只能设置一个小部件,因此您必须创建一个充当容器的QWidget,然后将按钮添加到其中。
class Test(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# ...
self.createButtons()
def createButtons(self):
# create the container and its layout
self.buttonContainer = QtWidgets.QWidget()
buttonLayout = QtWidgets.QHBoxLayout(self.buttonContainer)
# remove margins around the layout and set a minimal spacing between
# the children widgets
buttonLayout.setContentsMargins(0, 0, 0, 0)
buttonLayout.setSpacing(1)
# QToolButtons are usually better for this, as QPushButtons tend
# to expand themselves
self.minimizeButton = QtWidgets.QToolButton(text='_')
self.maximizeButton = QtWidgets.QToolButton(text='o')
self.closeButton = QtWidgets.QToolButton(text='x')
buttonLayout.addWidget(self.minimizeButton)
buttonLayout.addWidget(self.maximizeButton)
buttonLayout.addWidget(self.closeButton)
# set the container as the corner widget; as the docs explain,
# despite using "TopRightCorner", only the horizontal element (right
# in this case) will be used
self.tabWidget.setCornerWidget(
self.buttonContainer, QtCore.Qt.TopRightCorner)