Skip to content

Instantly share code, notes, and snippets.

@marcoshipe
Created April 4, 2018 04:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcoshipe/81473609c0927bea0a17828de29cc2a9 to your computer and use it in GitHub Desktop.
Save marcoshipe/81473609c0927bea0a17828de29cc2a9 to your computer and use it in GitHub Desktop.
pyqtSlot and garbage collector problem examples
import QtQuick 2.9
import QtQuick.Controls 2.2
ApplicationWindow {
id: main_window
visible: true
width: 500
height: 500
Item {
id: item1
Component.onCompleted: {
var lista = prueba_manager.data_request()
console.log(lista)
console.log(lista[0])
console.log(lista[0]['text'])
}
}
}
import sys
from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine, QQmlListProperty
class PruebaObject(QObject):
def __init__(self, text):
QObject.__init__(self)
self._text = text
@pyqtProperty('QString')
def text(self):
return self._text
@text.setter
def text(self, text):
self._text = text
class PruebaManager(QObject):
def __init__(self):
QObject.__init__(self)
@pyqtSlot(result=list)
def data_request(self):
aux = [PruebaObject('hi'), PruebaObject('bye')]
return aux
def main():
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
prueba_manager = PruebaManager()
engine.rootContext().setContextProperty("prueba_manager", prueba_manager)
engine.load("main.qml")
engine.quit.connect(app.quit)
sys.exit(app.exec_())
if __name__ == '__main__':
main()
qml: [,]
qml: null
file:.../main1.qml:17: TypeError: Cannot read property 'text' of null
import sys
from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine, QQmlListProperty
class PruebaObject(QObject):
def __init__(self, text):
QObject.__init__(self)
self._text = text
@pyqtProperty('QString')
def text(self):
return self._text
@text.setter
def text(self, text):
self._text = text
class PruebaManager(QObject):
def __init__(self):
QObject.__init__(self)
@pyqtSlot(result=QQmlListProperty)
def data_request(self):
aux = [PruebaObject('hi'), PruebaObject('bye')]
return QQmlListProperty(PruebaObject, self, aux)
def main():
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
prueba_manager = PruebaManager()
engine.rootContext().setContextProperty("prueba_manager", prueba_manager)
engine.load("main.qml")
engine.quit.connect(app.quit)
sys.exit(app.exec_())
if __name__ == '__main__':
main()
qml: QVariant(QQmlListProperty<QObject>)
qml: undefined
file:.../main1.qml:17: TypeError: Cannot read property 'text' of undefined
import sys
from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine, QQmlListProperty
class PruebaObject(QObject):
def __init__(self, text):
QObject.__init__(self)
self._text = text
@pyqtProperty('QString')
def text(self):
return self._text
@text.setter
def text(self, text):
self._text = text
class PruebaManager(QObject):
def __init__(self):
QObject.__init__(self)
@pyqtProperty(QQmlListProperty)
def data_request(self):
aux = [PruebaObject('hi'), PruebaObject('bye')]
return QQmlListProperty(PruebaObject, self, aux)
def main():
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
prueba_manager = PruebaManager()
engine.rootContext().setContextProperty("prueba_manager", prueba_manager)
engine.load("main3.qml")
engine.quit.connect(app.quit)
sys.exit(app.exec_())
if __name__ == '__main__':
main()
import QtQuick 2.9
import QtQuick.Controls 2.2
ApplicationWindow {
id: main_window
visible: true
width: 500
height: 500
Item {
id: item1
Component.onCompleted: {
var lista = prueba_manager.data_request
console.log(lista)
console.log(lista[0])
console.log(lista[0]['text'])
}
}
}
qml: [object Object]
qml: PruebaObject(0x2714a50)
qml: hi
import sys
from PyQt5.QtCore import QObject, pyqtSlot, pyqtProperty
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine, QQmlListProperty
class PruebaObject(QObject):
def __init__(self, text):
QObject.__init__(self)
self._text = text
@pyqtProperty('QString')
def text(self):
return self._text
@text.setter
def text(self, text):
self._text = text
class PruebaManager(QObject):
def __init__(self):
QObject.__init__(self)
@pyqtSlot(result=list)
def data_request(self):
self.aux = [PruebaObject('hi'), PruebaObject('bye')]
return self.aux
def main():
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
prueba_manager = PruebaManager()
engine.rootContext().setContextProperty("prueba_manager", prueba_manager)
engine.load("main.qml")
engine.quit.connect(app.quit)
sys.exit(app.exec_())
if __name__ == '__main__':
main()
qml: [PruebaObject(0x10dbde0),PruebaObject(0x10dbe90)]
qml: PruebaObject(0x10dbde0)
qml: hi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment