2014-09-18 36 views
2

我使用PySide不同的對象地址,我得到一個警告,當我嘗試到電子表格設置爲某個Widget:警告似乎指向

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

我已經精確定位問題的位置固定它,但我很驚訝地看到,調用setStyleSheet()對象的地址是不同的:

<views.hierarchy.TreeView object at 0x7f3ab8139440> 

理想的,當我得到像上述警告我希望我可以取消對它的引用像here和了解更多原因。

我的問題:

  • 爲什麼兩個地址不同?

  • 有沒有什麼辦法從小部件 對象的警告中獲取地址?

  • 有什麼辦法直接解除引用警告中的地址嗎?

+0

是一個地址的部件和一個樣式表? – mdurant 2014-09-18 15:40:15

+0

@mdurrant「樣式表」只是一個字符串。不,該字符串的地址將是第三個單獨的值。此外,警告聲稱「...的小部件0x1cbecc0」... – 2014-09-18 15:46:14

回答

0

它看起來像原來的警告來自Qt,所以消息中給出的小部件地址是基礎C++對象。你顯示的其他消息大概來自python,因此顯示了pyside包裝器對象的地址。

可以使用shiboken module(或sip module的PyQt的),以獲得關於底層C++對象的信息:

>>> 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 

gc module梳理這一點,就可以找到從C python封裝對象++地址與這樣的功能:

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 
+0

謝謝!這正是我需要的;我知道'gc.get_objects()',但不能將它們鏈接到C++地址。 – 2014-09-19 08:41:46