You are not logged in.

#1 2008-06-02 03:30:00

solarwind
Member
From: Toronto
Registered: 2008-03-18
Posts: 546

PyQt4 General Threading Question

Hey all, I'm trying to make a Python + PyQt4 program where I sort of "launch" the GUI from the program. The GUI has some textboxes and labels and such. However, the main program has to be doing its own thing and UPDATE the GUI. That means that the GUI has to run in a separate thread (I think). However, I don't know how to access the widgets in the GUI if the GUI has its own thread.

For example, the main application has a constant job. It has to update the GUI during that job (when something triggers within the job). My question is, how do I access the textboxes and labels in the (separately threaded?) GUI from the main application's thread.

Offline

#2 2008-06-02 08:08:24

ornitorrincos
Forum Fellow
From: Bilbao, spain
Registered: 2006-11-20
Posts: 198

Re: PyQt4 General Threading Question

in the main application you can send custom signals that are received by some slots in the gui, the slots actuate only when they receive the signal and then they call to a function(this can be defined by you) this function can set the text of the labels or anything else.

I couldn't resist to put this, I know is a bit of self-promotion, but I encountered just the same thing
http://pymusicpd.cvs.sourceforge.net/pymusicpd/

any questions just ask.


-$: file /dev/zero
/dev/zero: symbolic link to '/dev/brain'

Offline

#3 2008-06-02 12:08:01

solarwind
Member
From: Toronto
Registered: 2008-03-18
Posts: 546

Re: PyQt4 General Threading Question

Wow, thanks!

Offline

#4 2008-06-02 18:24:46

TheSaint
Member
From: my computer
Registered: 2007-08-19
Posts: 1,523

Re: PyQt4 General Threading Question

ornitorrincos wrote:

any questions just ask.

I like python, but I'm facing a long learning curve smile Maybe because of laziness wink
I'd like to know how to implement a file browsing, in the way that to set a function which will return a file name.
I know about QtGui.QFileDialog.getOpenFileName(), but how to get this alive still misterious for me.


do it good first, it will be faster than do it twice the saint wink

Offline

#5 2008-06-02 19:29:25

solarwind
Member
From: Toronto
Registered: 2008-03-18
Posts: 546

Re: PyQt4 General Threading Question

ornitorrincos wrote:

in the main application you can send custom signals that are received by some slots in the gui, the slots actuate only when they receive the signal and then they call to a function(this can be defined by you) this function can set the text of the labels or anything else.

I couldn't resist to put this, I know is a bit of self-promotion, but I encountered just the same thing
http://pymusicpd.cvs.sourceforge.net/pymusicpd/

any questions just ask.

Ok, so when I emit a signal from my main application, how do I set the text to be displayed on the GUI? Do you have any code examples?

Offline

#6 2008-06-02 21:17:34

ornitorrincos
Forum Fellow
From: Bilbao, spain
Registered: 2006-11-20
Posts: 198

Re: PyQt4 General Threading Question

basically what you have is the main thread, which is the gui, and the rest of them which are the one that process the information.

in the main thread you start the gui and the secondary thread, there you have the slots declared so they get the different signals emitted from the main or secondary threads

then the secondary thread emits a signal which is taken by a slot in the main thread(the gui) and that slots executes a function.

the key is to think in the gui as the main thread and from there launch all the secondary threads that make calculus.

first you make the thread and declare some custom signals(this signals will be recollected by the slots in the main thread)

class example_thread(QThread):
    __pyqtsignals__ = ("int_signal(int)", "qstring_signal(QString)")

that will be the signals that you will emit.
then you declare the __init__ function:

    def __init__(self):
        QThread.__init__(self, parent=None)
        self.exiting= False

this will get executed when the thread starts so you can declare there some variables, anothe key function is __del__

    def __del__(self):
        self.exiting = True
        self.wait()

obviously is executed when the thread stops, something that never happens if it has a while True inside smile
the you have the run function:

    def run(self):
        code to run

inside you put all the code to run(I made all the code in functions so this part was small and more easy to make changes to the order in which the code runs), for example, I have a functions for emitting the signal to the main thread

and finally the most important one, this function can have any name but it must have inside self.start()
this function is the one to launch from the main thread, if we make it like

    def naming(self):
        self.start()

in the main thread(the StartQt4 class) we must put somewhere

self.thread = example_thread()
self.thread.naming()

self.thread = example_thread() is there because when assigning signals to slots we must provide who emits that signal(as with any other widget in qt)

I hope I have explained it fine(and yes, I know that the code needs some comments)

Last edited by ornitorrincos (2008-06-02 21:21:46)


-$: file /dev/zero
/dev/zero: symbolic link to '/dev/brain'

Offline

#7 2008-06-02 23:56:44

solarwind
Member
From: Toronto
Registered: 2008-03-18
Posts: 546

Re: PyQt4 General Threading Question

Wow, thanks for taking the time to explain all this. I'll try and tell you how it goes.

Offline

#8 2008-06-03 08:13:32

TheSaint
Member
From: my computer
Registered: 2007-08-19
Posts: 1,523

Re: PyQt4 General Threading Question

ornitorrincos wrote:

I hope I have explained it fine(and yes, I know that the code needs some comments)

I'll spend some time to understand your code. I really grateful for these details.
Yesterday I realize some code the show me the file choser, but I can't get the value when the choise is done or cancelled.

#!/usr/bin/python

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class InputDialog(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file','/home/user')
        #self.label.setText(unicode(filename))

app = QtGui.QApplication(sys.argv)
icon = InputDialog()
icon.show()
app.exec_()

Can you tell me why when I finish the file choise I'll see an empty window?
Also I can't know about filename....
OK let me go back and study more smile


do it good first, it will be faster than do it twice the saint wink

Offline

#9 2008-06-03 12:57:35

ornitorrincos
Forum Fellow
From: Bilbao, spain
Registered: 2006-11-20
Posts: 198

Re: PyQt4 General Threading Question

sincerely, I haven't used a file chooser, but when I have time I'll try to do something with it.


-$: file /dev/zero
/dev/zero: symbolic link to '/dev/brain'

Offline

Board footer

Powered by FluxBB