PDA

View Full Version : QGraphicsScene with 4 QImages



Morgan Cormier
31st March 2011, 02:28
Hi,

I have been trying to do that for a whole afternoon without success. I have 4 qImages that I want to put in one QGraphicsScene (top left, top right, bottom left, bottom right).

Using a QBrush displays only one image since I can't change the position of the QBrush (setBackgroundBrush(QBrush))
Using Qpixmap won't help since I am working in a QThread.
I tried to use a QPainterPath but nothing is displayed, maybe because I am not in the main GUI.

Does one of you have any idea on how to do that?I didn't try to use paintevent(), can this help? if yes how?

Thank you so much for your help,
Morgan

ChrisW67
31st March 2011, 03:08
Caveat: I am not a QGraphicScene user

It seems that QGraphicsPixmapItem should be useful if you want the images in the scene.

If you want the four images to be a merged background image then you could always merge them into a single QImage using QPainter and put that in the background. Or override the QGraphicsScene::drawBackground method.

Morgan Cormier
1st April 2011, 01:37
Thank you for your help,

I can't use a QGraphicsPixmapItem since I am working in a QThread . QGraphicsPixmapItem can only be used in the main thread.

I will try to override the QGraphicsScene::drawBackground method. I've never done that before, do you have an exemple on how I should do? Should I initialise the QPainter with the 4 QImages before calling QGraphicsScene::drawBackground ?

Thank you.

JohannesMunk
1st April 2011, 12:06
You say you are in a QThread. Probably calculating those images you want to be displayed. That is perfectly fine.

Now you will not be able to display them directly from that thread, whatever method you choose. The actual painting has to be done from the gui thread.

But you can quite easily create a descendant of QObject and QGraphicsItem that has a slot where you update a pixmap-member that you draw in the paintEvent. To prevent simultaneous painting and write-access protect the pixmap with a mutex. Lock it when updating and when painting.

Instead of manually protecting the pixmap you could also rely on Qt's queued connections, which will be the default connection type for objects living in different threads anyway! Then the update signal will be executed in the context of the gui-thread and you can set the pixmap of a QGraphicsPixmapItem directly. Keep in mind that the QThread object itself is not 'living' in the thread. But with the 'new' way of using QThreads, namely not subclassing QThread but moving a worker class to a QThread-instance, that should not be a problem.

Whenever your thread has a pixmap ready it just signals your item the update.

You add 4 of these items to your scene and set their positions with setPos.

HIH

Joh