Beginner: Simple Output to a Dialog PyQt4

David Boddie david at boddie.org.uk
Fri Apr 20 21:15:00 EDT 2007


On Tuesday 17 April 2007 07:42, Glen wrote:

>   I've written a script in python and put together a simple QFrame with a
>   QTextBrowser with Designer.  I've translated the C++ into python using
>   puic4.

Just to avoid any misunderstanding: the form is actually stored as XML. You
can create C++ code with uic or Python code with pyuic4.

>   The .py file is called outputWin.py. My Script and its 
>   functions are in cnt.py.

OK. Ideally, your window will contain a button (or some other control) that
the user can click to execute the functions.

>   Finally, my main is in pball.py which follows 
>   here:
> import sys
> from PyQt4 import Qt, QtCore
> from outputWin import *
> from cnt import *
> if __name__ == "__main__":
> app = Qt.QApplication(sys.argv)
> window = Qt.QDialog()
> ui = Ui_Dialog()
> ui.setupUi(window)
> window.show()
> app.exec_()
> 
> I want to call my functions in cnt and have an output to my QDialog.  Can
> somebody give me a clue as to how to proceed?  I can't find good an easy
> tutorial for PyQt4 and I've never used Qt before.

If, for example, you included a push button (QPushButton) in the form you
created with Qt Designer, and called it executeButton, you could connect its
clicked() signal to a function in cnt by including the following line after
setting up the user interface:

  window.connect(ui.executeButton, SIGNAL("clicked()"), cnt.myFunction)

This assumes that your function is called myFunction(), of course.
However, you wouldn't be able to get the output from this function back to
the dialog just by using a signal-slot connection like this.

One way to solve this would be to wrap the function using another function
or instance that is able to modify the contents of the dialog. Another
cleaner approach would be to subclass the user interface class (Ui_Dialog)
and implement a custom slot that can both call the function and modify the
dialog.

For example:

class Dialog(QDialog, Ui_Dialog)

    def __init__(self, parent = None):
    
        QDialog.__init__(self, parent)
        self.setupUi(self)

        self.connect(self.executeButton, SIGNAL("clicked()"),
                     self.callFunction)

    def callFunction(self):
    
        data = cnt.myFunction()
        # Do something with the data.

Hope this gets you started,

David



More information about the Python-list mailing list