Reputation: 597
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.
Upvotes: 1
Views: 278
Reputation:
Checkout this example:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#---------
# IMPORT
#---------
from PyQt4 import QtGui, QtCore
#---------
# MAIN
#---------
class MyWindow(QtGui.QWidget):
valueChanged = QtCore.pyqtSignal(int)
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.value = 0
self.pushButtonAdd = QtGui.QPushButton(self)
self.pushButtonAdd.setText("Add!")
self.pushButtonAdd.clicked.connect(self.on_pushButtonAdd_clicked)
self.pushButtonSubtract = QtGui.QPushButton(self)
self.pushButtonSubtract.setText("Subtract!")
self.pushButtonSubtract.clicked.connect(self.on_pushButtonSubtract_clicked)
self.lineEditValue = QtGui.QLineEdit(self)
self.lineEditValue.setText(str(self.value))
self.layoutVertical = QtGui.QVBoxLayout(self)
self.layoutVertical.addWidget(self.pushButtonAdd)
self.layoutVertical.addWidget(self.pushButtonSubtract)
self.layoutVertical.addWidget(self.lineEditValue)
@QtCore.pyqtSlot()
def on_pushButtonAdd_clicked(self):
self.valueChanged.emit(1)
@QtCore.pyqtSlot()
def on_pushButtonSubtract_clicked(self):
self.valueChanged.emit(-1)
def setValue(self, value):
self.value += value
self.lineEditValue.setText(str(self.value))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow()
main.resize(333, 111)
main.valueChanged.connect(main.setValue)
main.show()
sys.exit(app.exec_())
In this example, to change the value manually you can call main.setValue(34)
Upvotes: 1