Warning seems to point to a different object address

拜拜、爱过 提交于 2019-12-23 23:39:45

问题


I'm using PySide and I get a warning when I try to set a spreadsheet to a certain widget:

09-18 14:48:54,107 WARNING  [D=0x7ff4a0074650:qt] Could not parse stylesheet of widget 0x1cbecc0

I've pinpointed the location of the problem and fixed it, but I was surprised to see that the address of the object calling setStyleSheet() is different:

<views.hierarchy.TreeView object at 0x7f3ab8139440>

Ideally, when I get warnings like the above I wish I could dereference it like here and find out more about the cause.

My questions:

  • Why are the two addresses different?

  • Is there any way to get the address in the warning from the widget object?

  • Is there any way to dereference the address in the warning directly?


回答1:


It looks like the original warning is coming from Qt, and so the widget address given in the message is for the underlying C++ object. The other message you've shown is presumably from python, and therefore shows the address of the pyside wrapper object.

You can use the shiboken module (or the sip module for PyQt) to get information about the underlying C++ objects:

>>> import shiboken
>>> from PySide import QtGui
>>> app = QtGui.QApplication([])
>>> w = QtGui.QWidget()
>>> repr(w)
'<PySide.QtGui.QWidget object at 0x7f7398c1d950>'
>>> w.setStyleSheet('{')
Could not parse stylesheet of widget 0x12e2fc0
>>> print(shiboken.dump(w))
C++ address....... PySide.QtGui.QWidget/0x12e2fc0 
hasOwnership...... 1
containsCppWrapper 1
validCppObject.... 1
wasCreatedByPython 1

>>> hex(shiboken.getCppPointer(w)[0])
>>> 0x12e2fc0

Combing this with the gc module, it is possible to find the python wrapper object from the C++ address with a function like this:

import gc, shiboken

def wrapper_from_address(address):
    for item in gc.get_objects():
        try:
            if address == shiboken.getCppPointer(item)[0]:
                return item
        except TypeError:
            pass


来源:https://stackoverflow.com/questions/25916919/warning-seems-to-point-to-a-different-object-address

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!