本文介绍了警告似乎指向不同的对象地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PySide,当我尝试将电子表格设置为某个小部件时收到警告:

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

我已经查明了问题的位置并修复了它,但我惊讶地发现调用setStyleSheet()的对象的地址不同:

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.

我的问题:

  • 为什么这两个地址不同?

  • Why are the two addresses different?

有没有办法从widget获取警告中的地址对象?

Is there any way to get the address in the warning from the widgetobject?

有什么办法可以直接解引用警告中的地址?

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

推荐答案

看起来原始警告来自 Qt,因此消息中给出的小部件地址是针对底层 C++ 对象的.您显示的另一条消息可能来自 python,因此显示了 pyside 包装器对象的地址.

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.

您可以使用 shiboken 模块(或 sip 模块 for PyQt) 以获取有关底层 C++ 对象的信息:

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

'>>>w.setStyleSheet('{')无法解析小部件 0x12e2fc0 的样式表>>>打印(shiboken.dump(w))C++地址....... PySide.QtGui.QWidget/0x12e2fc0拥有所有权...... 1包含 CppWrapper 1有效CppObject.... 1wasCreatedByPython 1>>>十六进制(shiboken.getCppPointer(w)[0])>>>0x12e2fc0

>>> 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/0x12e2fc0hasOwnership...... 1containsCppWrapper 1validCppObject.... 1wasCreatedByPython 1>>> hex(shiboken.getCppPointer(w)[0])>>> 0x12e2fc0

将其与 gc 模块 结合起来,这是可能的使用如下函数从 C++ 地址中查找 python 包装器对象:

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

这篇关于警告似乎指向不同的对象地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:43