1
我有一個QPixmap
子額外類方法make
:鑄造一個QObject子類的實例
class Screenshot(QtGui.QPixmap):
@classmethod
def make(cls):
desktop_widget = QtGui.QApplication.desktop()
image = cls.grabWindow(
desktop_widget.winId(), rect.x(), rect.y(), rect.width(), rect.height())
import ipdb; ipdb.set_trace()
image.save()
return image
當我打電話Screenshot.make()
正確的類cls
傳遞,而是通過cls.grabWindow
創建的實例不是Screenshot
:
ipdb> ...py(30)make()
29 import ipdb; ipdb.set_trace()
---> 30 image.save()
31 return image
ipdb> cls
<class 'viewshow.screenshot.Screenshot'>
ipdb> image
<PyQt4.QtGui.QPixmap object at 0x7f0f8c4a9668>
更短:
ipdb> Screenshot.grabWindow(desktop_widget.winId())
<PyQt4.QtGui.QPixmap object at 0x7f0f8154c438>
如何獲得Screenshot
實例?
看起來不錯,但不工作:後'圖片= CLS(圖片)''圖像.size()'爲空。可以在PyQt中的錯誤。現在'image .__ class__ = cls'起作用。 – warvariuc
哦,'image = cls(image)'調用我的'__init__',而不是從C++重載。 – warvariuc
@warvariuc。我的答案完美無缺。你的'__init__'函數(在你的問題中沒有顯示)顯然被破壞了,可能是因爲它沒有正確調用基類「__init__」。 – ekhumoro