Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

applying python functions directly to Qt designer as signals

I am new to Qt and GUI programming overall but i have done a fair bit of coding in python - writing modules and so on. I need to develop simple GUIs for some of my old modules.

What i am trying to do can be represented by the following simple example:

def f(x, y):
    z = x + y
    return z

For this function i will give two line-edits for x and y and one for z. Now i create a push-button 'calculate' and when i do that i want it to take x and y from the line-edits run the function f(x,y) and give the output to z.

Is there any way to do this directly in Qt Designer by adding the function f(x,y) written in python?

If not what are the alternatives?

like image 640
linuS Avatar asked Dec 25 '11 17:12

linuS


People also ask

Can I use Python in Qt Creator?

Currently, Qt Creator allows you to create Python files (not projects) and run them. It also has syntax highlighting, but it lacks more complex features such as autocomplete. Now, go to File->New File or Project->Python and select Python source file. To run the created script: Tools->External->Python->RunPy.

Can I use Qt Designer with PySide2?

PySide2 Tutorial — Creating applications with Qt Designer The good news is that Qt comes with a graphical editor — Qt Designer — which contains a drag-and-drop UI editor. Using Qt Designer you can define your UIs visually and then simply hook up the application logic later.


1 Answers

The basic workflow when writing a PyQt4 gui is:

  1. Design the UI using Qt Designer.
  2. Generate a Python module from the UI file using pyuic4.
  3. Create an Application module for the main program logic.
  4. Import the GUI class into the Application module.
  5. Connect the GUI to the program logic.

So, given the UI file calc.ui, you could generate the UI module with:

pyuic4 -w calc.ui > calc_ui.py

And then create an application module something like this:

from PyQt4 import QtGui, QtCore
from calc_ui import CalculatorUI

class Calculator(CalculatorUI):
    def __init__(self):
        CalculatorUI.__init__(self)
        self.buttonCalc.clicked.connect(self.handleCalculate)

    def handleCalculate(self):
        x = int(self.lineEditX.text())
        y = int(self.lineEditY.text())
        self.lineEditZ.setText(str(x + y))

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Calculator()
    window.show()
    sys.exit(app.exec_())

Note that it helps to set the objectName for every widget in Designer's Property Editor so that they can be easily identified later on. In particular, the objectName of the main form will become class-name of the GUI class that is imported (assuming the "-w" flag to pyuic4 is used).

like image 166
ekhumoro Avatar answered Sep 26 '22 23:09

ekhumoro