I want to create a simple programm (in QtDesigner) that executes a function when button is clicked, and displays the value of this function in a LineEdit. And here is my code:
class MyForma1(object):
def AddWidgets1(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(613, 545)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(244, 352, 111, 51))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.lineEdit = QtGui.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(242, 290, 111, 20))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Form.changeText)
def retranslateUi(self, Form):
self.pushButton.setText(_translate("Form", "Click me", None))
self.lineEdit.setText(_translate("Form", "functionvalue", None))
class MyForma2(QtGui.QDialog, MyForma1):
def __init__(self, z11=0):
QtGui.QDialog.__init__(self)
self.AddWidgets1(self)
self.z = z11
def myfunc1(self):
self.z+=1
def changeText(self):
self.myfunc1()
self.lineEdit.setText(str(self.z))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Forma = MyForma2()
Forma.show()
sys.exit(app.exec_())
Actually, it works fine, but i dont like the way it is done, i want to make it more exquisite. The problem here, is that the button "executes" a function and value translation together. And i think it would be better if the button executes only a function and additionaly there is something that constantly translates the value of this function to LineEdit separately from the button. For example, there could be a situation when the function value, which needs to be constantly monitored, could be affected not only by the button, but also by some other events (ex: incoming signal from COM-port). And in this case, it would be great to emmit a signal every time the function is changed, not only when button is pressed.