0

I have been trying to make a Widget, having QVBoxLayout as a layout, which attaches QTabWidget. The tabs are not displaying any content and all I can see is white boxes. Here's the code:

Imports:

from PySide6.QtWidgets import (QMessageBox, QApplication, QWidget, QGridLayout, QLabel, QMainWindow, QTabWidget, QVBoxLayout)
import sys

First Tab:

class WelPage(QWidget):

    def __init__(self):
        super().__init__()
        wel_tab = QWidget()
        grid = QGridLayout()

        lab_name = QLabel("This is a label")
        git_link = QLabel("This is a link")
        git_link.setOpenExternalLinks(True)
        yt_link = QLabel("Another Link")
        yt_link.setOpenExternalLinks(True)

        grid.addWidget(lab_name, 0, 1)
        grid.addWidget(git_link, 1, 0)
        grid.addWidget(yt_link, 1, 3)

        wel_tab.setLayout(grid)
        wel_tab.show()

Second Tab:

class AboutPage(QWidget):

    def __init__(self):
        super().__init__()
        about_tab = QWidget()
        lo = QVBoxLayout()
        purpose = QLabel("A really long label")

        lo.addWidget(purpose)
        about_tab.setLayout(lo)
        about_tab.show()

Main function:

def main():
    w = QWidget()
    layout = QVBoxLayout()
    tw = QTabWidget()
    w.resize(450, 250)
    w.setWindowTitle('Window Title')
    layout.addWidget(tw)

    tw.addTab(WelPage(), "Welcome Screen")
    tw.addTab(AboutPage(), "About")
    tw.show()


    w.setLayout(layout)
    w.show()
    app.exec()



if __name__ == "__main__":
    app = QApplication(sys.argv)
    main()

Here is the output I get for both tabs:

Window SC

  • 2
    You're setting the layout on a widget that is not shown (and also deleted as there's no reference). Remove both those `about_tab` and `wel_tab`, add the `self` arguments to both layout constructors (or use `self.setLayout()`). You also don't need to call `show()`, as it's implicit when adding tabs. – musicamante Nov 23 '21 at 13:09
  • Ah okay that did work. What I initially did was write the tabs in a normal function (initialized everything using a variable) and that didn't work. So, I am guessing QTabWidget must have a class construction to work? Thank you for the help. Can't believe it was such a small issue. – Aadil Faizal Nov 23 '21 at 13:18
  • 1
    I'm not sure I'm understanding what you did before, but you probably used a local variable that got garbage collected before being properly added to the tab widget. You don't need a "class constructor", as you can add tabs just by using standard Qt classes and without subclassing: you just need to add widgets/tabs in the proper way. – musicamante Nov 23 '21 at 13:23
  • Yes, initially, I did use a local variable to initialize QTabWidget and QWidget, that too in a normal function. I did not use a class at all. In a nutshell, the two class in the question - they were a python function at first. But that wasn't working at all so I converted them into a class. Not sure how GC works so I shall take your word up for it now. As for the 'proper way', I think I have a lot to learn. If you help explain why creating the functions didn't/won't work, please do. Else, i am really thankful for your help :) – Aadil Faizal Nov 23 '21 at 13:34
  • GC works by deleting an object as soon as there's no reference left to it. Unfortunately, without knowing what you actually tried to do, I cannot tell you what went wrong, but, unless you need special behavior or you prefer a more structured code, creating tabs in functions is perfectly fine without the need of subclassing. – musicamante Nov 24 '21 at 01:03
  • [Link to the question I had with code](https://stackoverflow.com/questions/70093497/pyside6-create-qtabwidget-with-function-rather-than-class) – Aadil Faizal Nov 24 '21 at 09:21

0 Answers0