-1

I've been trying to make an application using Pyside6 and can't seem to understand why we can't create a QDialog having QTabWidget with just functions. I am not sure if I am making a mistake somewhere so, here is the code I've wrote:

Imports:

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

2 Tabs function:

def WelPage():
    grid = QGridLayout()
    wel_tab = QWidget()
    wel_tab.setLayout(grid)

    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)


 def AboutPage():
    about_tab = QWidget()
    lo = QVBoxLayout()
    purpose = QLabel("A really long label")

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

And the 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(argv)
    main()

Output: SC of Blank canvas

All this does is render a blank Dialog. Not sure why that is. Why must I be forced to use a class rather than this method?

  • 1
    `addTab()` expects a QWidget as first argument, otherwise it will ignore the call (and return -1). Both functions return `None`, so you're adding nothing to the tab widget, as you're practically doing `tw.addTab(None, "Welcome Screen")`. Add `return wel_tab` to `WelPage` and `return about_tab` to `AboutPage`. – musicamante Nov 24 '21 at 20:24
  • @musicamante Thank you so much. Your answers have helped me massively :) – Aadil Faizal Dec 05 '21 at 12:25

1 Answers1

0

You seem to lack certain basics of how widgets work in Qt.
QWidgets are (generally) supposed to have a parent. So when you instantiate a child widget, you pass to the constructor the parent. This way Qt knows how that they should be displayed. I recommend you follow some tutorials on how to do basic GUIs with Qt in Python.

Then, because you will need to know the parent in the function, either you can use a global variable or pass it as a parameter to the function, like so :

def add_about_widget(parent):
    about_widget = QWidget(parent)  # <--- here !
    ...

If you want to have menu items, I recommend you use a QMenuBar. Also, Qt nicely create a window for you when you ask it to show() your main widget, but explicitly creating the main window makes things clearer in my opinion.

Lenormju
  • 4,078
  • 2
  • 8
  • 22
  • 1
    The problem is not related to the parent, but the fact that the functions returned `None`. Using QTabWidget the parent argument is almost useless, as it's required to use `addTab` with a proper QWidget argument. – musicamante Nov 24 '21 at 20:22