April 22, 2011

tclin1998 tclin1998
Lab Rat
12 posts

How to use QThread in this case

 

HI:

I want to use one QThread to interactively handle user’s inputs events (mouse, keyboard, etc..)
The other thread to do things like drawing, moving data between memory and disk, or heavy computation.

Could anyone kind enough to give me some hints?

Thanks!

-Todd

11 replies

April 22, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

Hi Todd,

user input is send via events to widgets, and widgets MUST live in the main thread. You could derive from QApplication and send all user input as custom events to objects inside a thread (or put them to a queue which is handleded in the thread), but widgets always live in the main thread. This also applies to drawing to the widgets. The only thing you can do inside a thread, is drawing inside a pixmap and blit this pixmap then inside the main thread.

Memory operations, computations etc can surely be done inside a thread. I suggest you read pepes wiki article [developer.qt.nokia.com] on that.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

April 22, 2011

ZapB ZapB
Robot Herder
1354 posts

To clarify what Gerolf said, he means that you can only draw into a QImage in a non-gui thread not into QPixmap. QPixmap is tied to the underlying windowing system which in turn ties it to the main GUI thread.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

April 22, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

Ups, I always take the wrong one of QPixmap and QImage … sorry :-)

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

January 27, 2013

alex.araratyan alex.araraty..
Lab Rat
11 posts

Qt reference says that QPixmap is intended for off-screen drawing. Can I use QImage for off-screen drawing from non-gui thread?

January 27, 2013

alex.araratyan alex.araraty..
Lab Rat
11 posts

And if I can use QImage for off-screen drawing as weel as QPixmap the what is the difference? The point is that currently I use QPixmap for off-screen drawing and I’m going to redesign my app to use non-gui thread for drawing. Does it mean that I will need to use QImage instead of QPixmap? Can I be sure that all methods of QPixmap I currently use can be found in QImage? If not – then what is the solution?

January 28, 2013

JKSH JKSH
Dinosaur Breeder
650 posts

Hello,

Hmm… the documentation is confusing when it describes QPixmap as “off-screen”, because you need to convert a QImage into a QPixmap to display it on-screen. Anyway:

  • QImage is a “data structure” class — it is an array of pixels in your program’s memory, so you can access a QImage from any thread in your program.
  • QPixmap is tied to your OS’s windowing system — its memory is managed by the OS itself, so you can only access a QPixmap from the GUI thread.

I’d recommend:

  1. Manipulate pixels using a QImage, in a non-GUI thread
  2. Pass the finished QImage into your GUI thread
  3. In the GUI thread, convert the QImage into a QPixmap, and display it

Edit: I also recommend starting a new thread when you have a question

March 12, 2013

alex.araratyan alex.araraty..
Lab Rat
11 posts

When redesigning my application to use non-gui thread for drawing I realized that I can’t use QBrush::setTexture(QPixmap) method any more, since QPixmap can’t be used from non-gui thread. Any idea how can I use pattern based brush for non-gui thread drawing?

March 13, 2013

Asperamanca Asperamanca
Hobby Entomologist
371 posts

There’s a lot of things you can’t do when you draw in any thread other than the main thread. Font rendering will work differently, for example. The QImage you generate could be huge, depending on your scene.

I recommend thinking twice three times before outsourcing GUI to another thread.

If you have a performance or responsiveness issue,

- Optimize your code as it runs in the GUI thread

- Consider using a different GUI engine (e.g. GraphicsView, OpenGL,…)

- See what non-GUI-related data preparations you might be able to put into a separate thread. However, even this is going to make your program much more complicated.

April 1, 2013

bjanuario bjanuario
Lab Rat
3 posts

Have o look at Qt::BlockingQueuedConnection

April 1, 2013

JKSH JKSH
Dinosaur Breeder
650 posts

bjanuario wrote:
Have o look at Qt::BlockingQueuedConnection
What do you wish to achieve with a blocking queued connection?

April 1, 2013

bjanuario bjanuario
Lab Rat
3 posts

you could use Qt::BlockingQueuedConnection as connection type to stop your worker thread until the user has entered data. Other thread also waits until the user has finished entering data.

JKSH wrote:
bjanuario wrote:
Have o look at Qt::BlockingQueuedConnection
What do you wish to achieve with a blocking queued connection?
 
  ‹‹ QtScript and ECMAScript version      Resources not accessible when running outside of qtcreator [SOLVED / Problem related to unsopported image file formats] ››

You must log in to post a reply. Not a member yet? Register here!