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

Qt-interest Archive, August 2007
QProgressBar in a separate thread?


Message 1 in thread

Hi Multithread developers,

I have a heavy job which needs a separate thread (QThread). In 
processing the job I wish see a progress (QProgressBar).
What I did:
1) I tried to create a QProgressBar, but at runtime, it crashes and says 
“Widgets must be created in the GUI thread”.
2) I tried to get a QProgressBar pointer from MainWindow, and manipulate 
it. It crashes too, and says ”Cannot set events to objects owned by a 
different thread”
Do you have a solution?

Thanks in advance,
Lingfa


--
 [ signature omitted ] 

Message 2 in thread

> Hi Multithread developers,
>
> I have a heavy job which needs a separate thread (QThread). In
> processing the job I wish see a progress (QProgressBar).
> What I did:
> 1) I tried to create a QProgressBar, but at runtime, it crashes and says
> ?Widgets must be created in the GUI thread?.
> 2) I tried to get a QProgressBar pointer from MainWindow, and manipulate
> it. It crashes too, and says ?Cannot set events to objects owned by a
> different thread?
> Do you have a solution?

Use a cross thread signal and slot connection to set the progress on the bar.

--Justin

--
 [ signature omitted ] 

Message 3 in thread

On Tuesday 28 August 2007 19:58:52 Lingfa Yang wrote:
> Hi Multithread developers,
>
> I have a heavy job which needs a separate thread (QThread). In
> processing the job I wish see a progress (QProgressBar).
> What I did:
> 1) I tried to create a QProgressBar, but at runtime, it crashes and says
> âWidgets must be created in the GUI threadâ.
> 2) I tried to get a QProgressBar pointer from MainWindow, and manipulate
> it. It crashes too, and says âCannot set events to objects owned by a
> different threadâ
> Do you have a solution?

Create the QProgressBar in the main thread and control it only from the main 
thread.

From your separate thread, post an even to a listening object or emit a signal 
that contains the progress information. The main thread will receive that 
signal or event and will then update the progress bar.

Since QProgressBar::setValue is a slot, you can connect your signal directly 
to it. Make sure the signal delivery is happening via a queued connection 
(the default settings for QObject::connect() will do that).

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 4 in thread

Thiago Macieira wrote:

>On Tuesday 28 August 2007 19:58:52 Lingfa Yang wrote:
>  
>
>>Hi Multithread developers,
>>
>>I have a heavy job which needs a separate thread (QThread). In
>>processing the job I wish see a progress (QProgressBar).
>>What I did:
>>1) I tried to create a QProgressBar, but at runtime, it crashes and says
>>“Widgets must be created in the GUI thread”.
>>2) I tried to get a QProgressBar pointer from MainWindow, and manipulate
>>it. It crashes too, and says ”Cannot set events to objects owned by a
>>different thread”
>>Do you have a solution?
>>    
>>
>
>Create the QProgressBar in the main thread and control it only from the main 
>thread.
>
>From your separate thread, post an even to a listening object or emit a signal 
>that contains the progress information. The main thread will receive that 
>signal or event and will then update the progress bar.
>
>Since QProgressBar::setValue is a slot, you can connect your signal directly 
>to it. Make sure the signal delivery is happening via a queued connection 
>(the default settings for QObject::connect() will do that).
>
>  
>

Thiago and Justin,

I did it as you told me. It works. 

Thank you both for help.
Lingfa



--
 [ signature omitted ] 

Message 5 in thread

Create a function in the thread that tells the threads status.  In the
main thread (gui thread), call that function, get the status and update
the progress bar accordingly.

-----Original Message-----
From: Lingfa Yang [mailto:lingfa@xxxxxxx] 
Sent: Tuesday, August 28, 2007 10:59
To: Qt Interest
Subject: QProgressBar in a separate thread?


Hi Multithread developers,

I have a heavy job which needs a separate thread (QThread). In 
processing the job I wish see a progress (QProgressBar).
What I did:
1) I tried to create a QProgressBar, but at runtime, it crashes and says

"Widgets must be created in the GUI thread".
2) I tried to get a QProgressBar pointer from MainWindow, and manipulate

it. It crashes too, and says "Cannot set events to objects owned by a 
different thread"
Do you have a solution?

Thanks in advance,
Lingfa


--
 [ signature omitted ] 

Message 6 in thread

Jones, Torrin A (US SSA) wrote:

> Create a function in the thread that tells the threads status.  In the
> main thread (gui thread), call that function, get the status and update
> the progress bar accordingly.
That sounds like polling, and is in my view quite dirty. I'd go with the
already given solution: just send off a signal from your worker to your gui
thread and connect them using a queued connection.

André
-- 
 [ signature omitted ]