was trying to understand https://wiki.python.org/moin/PyQt/Binding%20widget%20properties%20to%20Python%20variables :
"Binding widget properties to Python variables"
down below my modified code that took a while to me, but kind of visualize better what bind
, example code, here, does:
def bind(objectName, propertyName, type):
def getter(self):
return type(self.findChild(QObject, objectName).property(propertyName).toPyObject())
def setter(self, value):
self.findChild(QObject, objectName).setProperty(propertyName, QVariant(value))
return property(getter, setter)
my complete code is:
import sys
from PyQt5.QtWidgets import QWidget, QLineEdit, QTextEdit, QCheckBox, QFormLayout, QPushButton
from PyQt5 import QtWidgets
def bind(objectName, propertyName, type):
def getter(self):
return type(self.findChild(QObject, objectName).property(propertyName).toPyObject())
def setter(self, value):
self.findChild(QObject, objectName).setProperty(propertyName, QVariant(value))
return property(getter, setter)
class Window(QWidget):
def __init__(self, parent = None):
super().__init__(parent)
self.nameEdit = QLineEdit()
self.nameEdit.setObjectName("nameEdit")
self.addressEdit = QTextEdit()
self.addressEdit.setObjectName("addressEdit")
self.contactCheckBox = QCheckBox()
self.contactCheckBox.setObjectName("contactCheckBox")
self.button_1 = QPushButton('press me !!!')
self.button_1.clicked.connect(self.pushButton_1_Pressed)
self.button_2 = QPushButton('press me !!! second')
self.button_2.clicked.connect(self.pushButton_2_Pressed)
self.layout = QFormLayout(self)
self.layout.addRow(self.tr("Name:"), self.nameEdit)
self.layout.addRow(self.tr("Address:"), self.addressEdit)
self.layout.addRow(self.tr("Receive extra information:"), self.contactCheckBox)
self.layout.addWidget(self.button_1)
self.layout.addWidget(self.button_2)
self.setLayout(self.layout)
self.name = bind('nameEdit', 'text', str)
self.address = bind("addressEdit", "plainText", str)
self.contact = bind("contactCheckBox", "checked", bool)
def pushButton_1_Pressed(self):
print(self.nameEdit.text())
print(self.addressEdit.toPlainText())
print(self.contactCheckBox.isChecked())
def pushButton_2_Pressed(self):
self.nameEdit.setText('pippo')
self.addressEdit.clear()
self.addressEdit.insertPlainText('papero')
self.contactCheckBox.setChecked(True)
print(self.nameEdit.text())
print(self.addressEdit.toPlainText())
print(self.contactCheckBox.isChecked())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
after you insert text into a QLineEdit
or QTextEdit
or check a QCheckBox
widget
you can print the variables defined by bind
and pressing the second button you change the variable values and the text/values of the widgets at the same time (got some insight from Binding a PyQT/PySide widget to a local variable in Python.
since the inners of Python and PyQt5 are to hard to me right know, is about a dumb-proof descriprion about how bind
works in the code on the three widgets.