Last active
November 9, 2017 02:52
-
-
Save rwarren/995e7ddc373f7119c51737fcd4aba0cc to your computer and use it in GitHub Desktop.
demo of objectName() not working with QML (for me?)
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 import QtCore, QtQml, QtWidgets | |
QML = b''' | |
import QtQuick 2.7 | |
import QtQuick.Window 2.2 | |
import QtQuick.Controls 1.4 | |
import ObjectNameTester 1.0 | |
Window { | |
visible: true | |
ObjectNameTester { | |
id: ont | |
objectName: "myONT" | |
} | |
Button { | |
text: "Do test" | |
onClicked: ont.interact() | |
} | |
} | |
''' | |
class ObjectNameTester(QtCore.QObject): | |
@QtCore.pyqtSlot() | |
def interact(self): | |
name = self.objectName() # expecting 'myONT' here, but it gets '' | |
print("objectName() is %r" % name) | |
if __name__ == '__main__': | |
app = QtWidgets.QApplication(sys.argv + ["-nograb"]) | |
QtQml.qmlRegisterType(ObjectNameTester, "ObjectNameTester", 1, 0, "ObjectNameTester") | |
engine = QtQml.QQmlApplicationEngine() | |
engine.loadData(QML) | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment