Skip to content

Instantly share code, notes, and snippets.

@rwarren
Last active November 9, 2017 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwarren/995e7ddc373f7119c51737fcd4aba0cc to your computer and use it in GitHub Desktop.
Save rwarren/995e7ddc373f7119c51737fcd4aba0cc to your computer and use it in GitHub Desktop.
demo of objectName() not working with QML (for me?)
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