Created
April 20, 2017 13:35
Why does the setter trigger multiple times.
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 | |
from PyQt5.QtWidgets import QApplication | |
class CustomType(QtCore.QObject): | |
names_changed = QtCore.pyqtSignal() | |
def __init__(self, parent = None): | |
super().__init__(parent) | |
self._names = [] | |
self._times_in_setter = 0 | |
@QtCore.pyqtProperty(QtCore.QVariant, notify = names_changed) | |
def names(self): | |
return self._names | |
@names.setter | |
def names(self, names): | |
names_list = names.toVariant() | |
# if names_list == self._names: | |
# return | |
self._times_in_setter += 1 | |
self._names = names_list | |
print(names) | |
print("objectName: ", self.objectName()) | |
print("names: ", self._names) | |
print("Times in setter: ", self._times_in_setter) | |
print() | |
self.names_changed.emit() | |
QML = b""" | |
import QtQuick 2.7 | |
import QtQuick.Controls 1.4 | |
import CustomType 1.0 | |
ApplicationWindow { | |
visible: true | |
width: 640 | |
height: 480 | |
Column { | |
ComboBox { | |
model: customType.names | |
} | |
ComboBox { | |
model: customType2.names | |
} | |
} | |
CustomType { | |
id: customType | |
objectName: "customType" | |
names: ["one", "two"] | |
} | |
CustomType { | |
id: customType2 | |
objectName: "customType2" | |
names: customType.names | |
} | |
} | |
""" | |
app = QApplication(sys.argv) | |
QtQml.qmlRegisterType(CustomType, 'CustomType', 1, 0, 'CustomType') | |
engine = QtQml.QQmlApplicationEngine() | |
engine.loadData(QML) | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment