skip to Main Content

In a PyQt application I receive this message:

Could not parse stylesheet of object QLabel(0x560f3d6914e0)

How can I find the exact location of the relevant statement in Visual Studio Code as I do not know which label it is?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to musicamante I could manage to implement the second solution that required some little corrections:

    from PyQt6 import sip
        def __init__(self, id,recipe_id,parent=None):
            ...
            
            QTimer.singleShot(100, self.showDebug)
    
        def showDebug(self):
            index, ok = QInputDialog.getText(self, 'Wrap label', 'Type the id')
            if ok:
                label = sip.wrapinstance(int(index,base=16), QWidget)
                if label:
                    print(label.objectName(), label.text(), label.styleSheet()) 
    

    But it only returned me the label.styleSheet probably due to the fact the error was in an imported module.


  2. The number you see is the memory address for the widget within the Qt space, and that error is just shown in the stdout, so it cannot be directly debugged nor referenced.

    In order to get the python object, you would need to wrap its instance using the functions provided by the sip module.

    Unfortunately, since that error is given at runtime, you cannot "hardcode" it and convert later, since memory addresses are not persistent.

    What you can do, though, is to run the code in an interactive python shell, and also import the sip module, then use wrapinstance():

    >>> import MyProgram
    >>> window = MyWindow()
    # the error will probably appear here
    Could not parse stylesheet of object QLabel(0x560f3d6914e0)
    
    >>> import sip
    >>> label = sip.wrapinstance(0x560f3d6914e0, QWidget)
    >>> if label: print(label.objectName(), label.text(), label.styleSheet())
    

    Alternatively, just add a temporary function that you can activate from a keyboard shortcut or button within your program (or even call it with QTimer) and use QInputDialog functions to do something similar to the above.

    class MyWindow(QMainWindow):
        def __init__(self, parent=None):
            ...
            QTimer.singleShot(100, self.showDebug)
    
        def showDebug(self):
            index, ok = QInputDialog.getText(self, 'Wrap label', 'Type the id')
            if ok:
                label = sip.wrapinstance(int(index, base=16), QWidget)
                if label:
                    print(label.objectName(), label.text(), label.styleSheet())
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search