Search code examples
pyqtpyqt5qt-designer

Qt Designer make widget overlap another widget in layout


I want to make 3 window button like above picture (similar Google Chrome) use Qt Designer and PyQt.

I want 3 buttons overlap right side of TabWidget. But I only can overlap the Button on TabWidget when break layout like the picture.

When I set any layout, every widget can not overlap on each other. So can I overlap when set layout? Thanks.

This is the layout I want

It similar Google Chrome's layout


Solution

  • This cannot be done in creator/designer, and can only be achieved using setCornerWidget() from your code.

    Since only one widget can be set for each corner, you have to create a QWidget that acts as a container, then add the buttons to it.

    corner widget example

    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)