Search code examples
pythonqtpyqt5qt5

Remove margin around QTabWidget in python


I am making a web browser in qt5 for fun, however there is a margin between the QTabWidget and the window, and does not look appealing. Is there any way to remove it?

Here is the app class code

class App(QWidget):
    def __init__(self) -> None:
        super().__init__()

        #self.windowTitle = "Test!"
        self.layout = QGridLayout()
        self.setLayout(self.layout)
        self.layout.setHorizontalSpacing(0)
        self.layout.setVerticalSpacing(0)

        self.tabs = []
        self.tabwidget = QTabWidget()
        self.layout.addWidget(self.tabwidget, 0, 0)

        self.add_tab("Home", "asd")

        self.showMaximized()
        
    def add_tab(self, title: str, url: str):
        tab = browser.tab.Tab()
        tab.load("https://www.google.com")
        self.tabwidget.addTab(tab, title)

Solution

  • As suggested in the comments, you can assign the specific margins using the a method of QLayout.

    For example to completely eliminate the margin altogether you can use:

    class App(QWidget):
        def __init__(self) -> None:
            super().__init__()
    
            #self.windowTitle = "Test!"
            self.layout = QGridLayout()
            self.setLayout(self.layout)
            self.layout.setHorizontalSpacing(0)
            self.layout.setVerticalSpacing(0)
    
            self.tabs = []
            self.tabwidget = QTabWidget()
            self.layout.addWidget(self.tabwidget, 0, 0)
    
            self.add_tab("Home", "asd")
            self.layout.setContentsMargins(0, 0, 0, 0)  # <--- added this
    
            self.showMaximized()
            
        def add_tab(self, title: str, url: str):
            tab = browser.tab.Tab()
            tab.load("https://www.google.com")
            self.tabwidget.addTab(tab, title)