PySide 6 create a class as a QLabel object not working

47 views Asked by At

I'm trying to make a custom QLabel class but I always got an error code -1073740791. Any idea how to make it work? Coding on PyCharm. Thanks

Code:

from PySide6.QtWidgets import*
class IconLabel(QLabel):

    def __init__(self):
        super().__init__()

lab = IconLabel()

Try to make a custom QLabel object to display specific icon on demand into it, but can't initiate the object before adding the function. Error code without details or info from the web.

2

There are 2 answers

3
GarageFab On

Ok so to init a QLabel you need to pass a widget parent to it and create a QApplication before it, then you declare the previous widget as parent:

from PySide6.QtWidgets import*                                                                                                  
class IconLabel(QLabel):

    def __init__(self, widget):
        super().__init__(parent=widget)                                                                                 

import sys                                                                                                                  
app = QApplication(sys.argv)
widget = QWidget()
lab = IconLabel(widget)

Thanks @mahkitah

1
mahkitah On

QWidgets need a QApplication running.

import sys
from PySide6.QtWidgets import*

class IconLabel(QLabel):
    def __init__(self):
        super().__init__()
        self.setText('hello')


app = QApplication(sys.argv)
lab = IconLabel()
lab.show()
sys.exit(app.exec())

If you look at other examples you'll see that they all have similar lines at the bottom. It's just a bit of boilerplate code to run a Qt app.