Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, August 2006
Updating GUI with a signal from a QThread


Message 1 in thread

For the last two days all I am doing is to read QThread related posts in 
the archives of this list. But still I have this quite basic problem.

I have a simple GUI with a QTextEdit.
In my work thread's run() function there is a long loop.
Inside run(), at every, let's say, 100 iterations I am sending a signal 
to the GUI thread.
This signal is connected to a slot that appends "Iteration: 100" to 
QTextEdit.
The problem is, I can not see these texts appearing one by one, but they 
all appear at once when run() finishes its job.

Cuneyt

--
 [ signature omitted ] 

Message 2 in thread

Cüneyt Sert said:
> For the last two days all I am doing is to read QThread related posts in
> the archives of this list. But still I have this quite basic problem.
>
> I have a simple GUI with a QTextEdit.
> In my work thread's run() function there is a long loop.
> Inside run(), at every, let's say, 100 iterations I am sending a signal
> to the GUI thread.
> This signal is connected to a slot that appends "Iteration: 100" to
> QTextEdit.
> The problem is, I can not see these texts appearing one by one, but they
> all appear at once when run() finishes its job.

If the signal comes from the QThread and the QThread was creates in the
GUI thread then you are probably lucky the application does not crash...

You have to chose a queued connection
(connect(....,QT::QueuedConnection)), so that the signal is relayed to the
correct thread.


   Konrad

--
 [ signature omitted ] 

Message 3 in thread

-----Original Message-----
From: Konrad Rosenbaum [mailto:konrad@xxxxxxxxx]
Sent: Thu 10.08.2006 18:32
To: qt-interest@xxxxxxxxxxxxx
Subject: Re: Updating GUI with a signal from a QThread
 
Cüneyt Sert said:
>> For the last two days all I am doing is to read QThread related posts in
>> the archives of this list. But still I have this quite basic problem.
>>
>> I have a simple GUI with a QTextEdit.
>> In my work thread's run() function there is a long loop.
>> Inside run(), at every, let's say, 100 iterations I am sending a signal
>> to the GUI thread.
>> This signal is connected to a slot that appends "Iteration: 100" to
>> QTextEdit.
>> The problem is, I can not see these texts appearing one by one, but they
>> all appear at once when run() finishes its job.

> If the signal comes from the QThread and the QThread was creates in the
> GUI thread then you are probably lucky the application does not crash...

What else can I do? I need a thread and I need that thread communicate with the GUI thread. This is exactly what they did in the Mandelbrot example that comes with Qt4.

>You have to chose a queued connection
>(connect(....,QT::QueuedConnection)), so that the signal is relayed to the
>correct thread.

I already tried this, but nothing changes.

--
 [ signature omitted ] 

Message 4 in thread

On 10.08.06 23:24:43, Cüneyt Sert wrote:
> -----Original Message-----
> From: Konrad Rosenbaum [mailto:konrad@xxxxxxxxx]
> Sent: Thu 10.08.2006 18:32
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: Updating GUI with a signal from a QThread
>  
> Cüneyt Sert said:
> >> For the last two days all I am doing is to read QThread related posts in
> >> the archives of this list. But still I have this quite basic problem.
> >>
> >> I have a simple GUI with a QTextEdit.
> >> In my work thread's run() function there is a long loop.
> >> Inside run(), at every, let's say, 100 iterations I am sending a signal
> >> to the GUI thread.
> >> This signal is connected to a slot that appends "Iteration: 100" to
> >> QTextEdit.
> >> The problem is, I can not see these texts appearing one by one, but they
> >> all appear at once when run() finishes its job.
> 
> > If the signal comes from the QThread and the QThread was creates in the
> > GUI thread then you are probably lucky the application does not crash...
> 
> What else can I do? I need a thread and I need that thread communicate with the GUI thread. This is exactly what they did in the Mandelbrot example that comes with Qt4.

Well basically this sounds like your worker thread doesn't really run in
it's own thread. You do start it via thread.start and not calling run()
directly?

Other than that, you could try to use Events for communication rather
than signals.

Andreas

-- 
 [ signature omitted ] 

Message 5 in thread

 
-----Original Message-----
>Cüneyt Sert said:
>For the last two days all I am doing is to read QThread related posts in
>the archives of this list. But still I have this quite basic problem.
>
>I have a simple GUI with a QTextEdit.
>In my work thread's run() function there is a long loop.
>Inside run(), at every, let's say, 100 iterations I am sending a signal
>to the GUI thread.
>This signal is connected to a slot that appends "Iteration: 100" to
>QTextEdit.
>The problem is, I can not see these texts appearing one by one, but they
>all appear at once when run() finishes its job.
=============
>If the signal comes from the QThread and the QThread was creates in the
>GUI thread then you are probably lucky the application does not crash...
=============
>What else can I do? I need a thread and I need that thread communicate
>with the GUI thread. This is exactly what they did in the Mandelbrot example
>that comes with Qt4.
=============
>Well basically this sounds like your worker thread doesn't really run in
>it's own thread. You do start it via thread.start and not calling run()
>directly?
>
>Other than that, you could try to use Events for communication rather
>than signals.
>
>Andreas
============

Yes, I am calling start(), not run(). Calling run() behaves quite strange.

Actuallly, I just realized that in the big loop inside run(), if I emit the signal NOT too frequently, for example not every 100 iterations but every 1000 iterations, the result is as expected. By expected I mean, the texts "Iteration: 1000", "Iteration: 2000", etc. appear in QTextEdit one by one. I simply think that when the signal frequency is above a certain limit, my computer just can not handle it and all the texts appear at once after run() finishes.

I'll also look into using events instead of signals.

Cuneyt

--
 [ signature omitted ] 

Message 6 in thread

On 11.08.06 01:13:47, Cüneyt Sert wrote:
> Actuallly, I just realized that in the big loop inside run(), if I emit the signal NOT too frequently, for example not every 100 iterations but every 1000 iterations, the result is as expected. By expected I mean, the texts "Iteration: 1000", "Iteration: 2000", etc. appear in QTextEdit one by one. I simply think that when the signal frequency is above a certain limit, my computer just can not handle it and all the texts appear at once after run() finishes.

Yes, that sounds reasonable. The reason why it works under linux could
be different things, but in general something "along the way" is faster.
That could be the Qt widget drawing faster on linux, or the
event-posting working better there. What happens for you on Windows is
probably that you send the first signal and the gui thread calls the
connected slot. The slot then changes the Ui which puts a repaint-event
into the event queue. Now before the next run of the qt event loop the
next signal arrives and the slot is called first, which triggers a new
repaint event which is combined with the first repaint and so on. Then
when the threads are done Qt has time to actually execute the event loop
and all the repaints happen. As I said it could be that the drawing is
to slow, or the thread implementation on windows is slow...

> I'll also look into using events instead of signals.

Not sure wether that'll help, as IIRC that's what Qt::QueuedConnection
does, it puts an event into the event queue that calls the connected
slot.

Andreas

-- 
 [ signature omitted ] 

Message 7 in thread

Hi,

If you need to force an update of the GUI, call qApp->processEvents().

Cheers

Alex.


-----Original Message-----
From: Cüneyt Sert [mailto:cuneyt@xxxxxxxxxxxxxx] 
Sent: Thursday, August 10, 2006 5:08 PM
To: qt-interest@xxxxxxxxxxxxx
Subject: Updating GUI with a signal from a QThread

For the last two days all I am doing is to read QThread related posts in 
the archives of this list. But still I have this quite basic problem.

I have a simple GUI with a QTextEdit.
In my work thread's run() function there is a long loop.
Inside run(), at every, let's say, 100 iterations I am sending a signal 
to the GUI thread.
This signal is connected to a slot that appends "Iteration: 100" to 
QTextEdit.
The problem is, I can not see these texts appearing one by one, but they 
all appear at once when run() finishes its job.

Cuneyt

--
 [ signature omitted ] 

Message 8 in thread

On 10.08.06 17:38:56, Benn Alex wrote:
> If you need to force an update of the GUI, call qApp->processEvents().

According to long-term kde and qt dev's this is not a good idea, unless
you know what you're doing. This can get you in all kinds of trouble,
unfortunately I don't remember any examples :-(

There are better ways to solve the ops problem.

Andreas

-- 
 [ signature omitted ] 

Message 9 in thread

 
>> If you need to force an update of the GUI, call qApp->processEvents().

>According to long-term kde and qt dev's this is not a good idea, unless
>you know what you're doing. This can get you in all kinds of trouble,
>unfortunately I don't remember any examples :-(

>There are better ways to solve the ops problem.
>Andreas


The reason I am writing to this list is to learn about those better ways :)
Cuneyt



--
 [ signature omitted ]