Beginner: Simple Output to a Dialog PyQt4

G

Glen

Hello,
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. The .py file is called outputWin.py. My Script and its
functions are in cnt.py. 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.
 
D

David Boddie

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
 
G

Glen

# Just to avoid any misunderstanding: the form is actually stored as XML.
# You can create C++ code with uic or Python code with pyuic4.
Right. I do remember noticing that when I opened one of the .ui files.

Thanks for the instructions. I'm tackling signals and slots next. I'll
be reading your post a few times, I'm sure. For the time being, just to
get myself off the ground and see some output, I imported my functions
from cnt.py into my main with 'from cnt import cnt'. Then I passed my
QTextEdit object into my python code and output the contents of my file
with:
f = file("filename", 'r')
for line in f:
QTxtObj.insertPlainText(line)

Maybe you could point out some problems with doing it this way, but I'm at
the point now where I have to learn how to handle signals and slots. I'm
setting up an input dialog with several options, such as download a URL,
choose an existing file.

Your information will come in handy.

Glen
[quoted text muted]

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top