By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

Beginner: Simple Output to a Dialog PyQt4

P: n/a
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.
Apr 17 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Apr 21 '07 #2

P: n/a
On Sat, 21 Apr 2007 03:15:00 +0200, David Boddie wrote:
On Tuesday 17 April 2007 07:42, Glen wrote:
>
# 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
Apr 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.