Created
April 4, 2018 04:58
-
-
Save marcoshipe/81473609c0927bea0a17828de29cc2a9 to your computer and use it in GitHub Desktop.
pyqtSlot and garbage collector problem examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
qml: [,] | |
qml: null | |
file:.../main1.qml:17: TypeError: Cannot read property 'text' of null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
qml: QVariant(QQmlListProperty<QObject>) | |
qml: undefined | |
file:.../main1.qml:17: TypeError: Cannot read property 'text' of undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
qml: [object Object] | |
qml: PruebaObject(0x2714a50) | |
qml: hi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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