from PyQt4.QtCore import QObject class MyObject(QObject): def __init__(self): self.field = 7 obj = MyObject() print(obj.field) obj.setObjectName("New object") >>> Traceback (most recent call last): >>> File "pyinit.py", line 9, in <module> >>> obj.setObjectName("New object") >>> RuntimeError: '__init__' method of object's base class (MyObject) not called.
... class MyObject(QObject): def __init__(self): QObject.__init__(self) ...
from PyQt4.QtGui import QApplication, QLabel def createLabel(): label = QLabel("Hello, world!") label.show() app = QApplication([]) createLabel() app.exec_()
from PyQt4.QtGui import QApplication, QLabel def createLabel(): label = QLabel("Hello, world!") label.show() return label app = QApplication([]) label = createLabel() app.exec_()
from PyQt4.QtCore import QTimer from PyQt4.QtGui import QApplication, QWidget app = QApplication([]) widget = QWidget() widget.setWindowTitle("Dead widget") widget.deleteLater() QTimer.singleShot(0, app.quit) # , app.exec_() # , deleteLater() print(widget.windowTitle()) >>> Traceback (most recent call last): >>> File "1_basic.py", line 20, in <module> >>> print(widget.windowTitle()) >>> RuntimeError: wrapped C/C++ object of type QWidget has been deleted
from PyQt4.QtCore import Qt, QTimer from PyQt4.QtGui import QApplication, QLabel, QLineEdit def onLineEditTextChanged(): print('~~~~ Line edit text changed') def onLabelDestroyed(): print('~~~~ C++ label object destroyed') def changeLineEditText(): print('~~~~ Changing line edit text') lineEdit.setText("New text") class Label(QLabel): def __init__(self): QLabel.__init__(self) self.setAttribute(Qt.WA_DeleteOnClose) self.destroyed.connect(onLabelDestroyed) def __del__(self): print('~~~~ Python label obj QLineEdit, - Label.ect destroyed') def setText(self, text): print('~~~~ Changing label text') QLabel.setText(self, text) def close(self): print('~~~~ Closing label') QLabel.close(self) app = QApplication([]) app.setQuitOnLastWindowClosed(False) label = Label() label.show() lineEdit = QLineEdit() lineEdit.textChanged.connect(onLineEditTextChanged) lineEdit.textChanged.connect(label.setText) QTimer.singleShot(1000, label.close) # QTimer.singleShot(2000, changeLineEditText) # . . QTimer.singleShot(3000, app.quit) app.exec_() print('~~~~ Application exited') >>> ~~~~ Closing label >>> ~~~~ C++ label object destroyed >>> ~~~~ Changing line edit text >>> ~~~~ Line edit text changed >>> ~~~~ Changing label text >>> Traceback (most recent call last): >>> File "2_reallife.py", line 33, in setText >>> QLabel.setText(self, text) >>> RuntimeError: wrapped C/C++ object of type Label has been deleted >>> ~~~~ Application exited >>> ~~~~ Python label object destroyed
PYSIDE = False USE_SINGLESHOT = True if PYSIDE: from PySide.QtCore import Qt, QTimer from PySide.QtGui import QApplication, QLineEdit else: from PyQt4.QtCore import Qt, QTimer from PyQt4.QtGui import QApplication, QLineEdit def onLineEditDestroyed(): print('~~~~ C++ lineEdit object destroyed') def onSelectionChanged(): print('~~~~ Pure C++ method selectAll() called') class LineEdit(QLineEdit): def __init__(self): QLineEdit.__init__(self) self.setText("foo bar") self.destroyed.connect(onLineEditDestroyed) #self.selectionChanged.connect(onSelectionChanged) def __del__(self): print('~~~~ Python lineEdit object destroyed') def clear(self): """Overridden Qt method """ print('~~~~ Overridden method clear() called') QLineEdit.clear(self) def purePythonMethod(self): """Pure python method. Does not override any C++ methods """ print('~~~~ Pure Python method called') self.windowTitle() # generate exception app = QApplication([]) app.setQuitOnLastWindowClosed(False) lineEdit = LineEdit() lineEdit.deleteLater() if USE_SINGLESHOT: #QTimer.singleShot(1000, lineEdit.clear) #QTimer.singleShot(1000, lineEdit.purePythonMethod) QTimer.singleShot(1000, lineEdit.selectAll) # pure C++ method else: timer = QTimer(None) timer.setSingleShot(True) timer.setInterval(1000) timer.start() #timer.timeout.connect(lineEdit.clear) #timer.timeout.connect(lineEdit.purePythonMethod) timer.timeout.connect(lineEdit.selectAll) # pure C++ method QTimer.singleShot(2000, app.quit) app.exec_() print('~~~~ Application exited')
Slot type | PyQt | PySide |
---|---|---|
C ++ object method | slot is turned off | slot is turned off |
pure python method or function | the fall | slot is turned off |
C ++ method - an object overloaded with Python-wrapper | the fall | the fall |
Source: https://habr.com/ru/post/210304/
All Articles