Results 1 to 4 of 4

Thread: QGraphicsScene with 4 QImages

  1. #1
    Join Date
    Mar 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsScene with 4 QImages

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QGraphicsScene with 4 QImages

    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.

  3. #3
    Join Date
    Mar 2011
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene with 4 QImages

    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.

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsScene with 4 QImages

    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
    Last edited by JohannesMunk; 1st April 2011 at 11:28. Reason: updated contents

Similar Threads

  1. Replies: 5
    Last Post: 22nd October 2010, 09:29
  2. Bug in QGraphicsScene?
    By tbcpp in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2009, 14:23
  3. in QGraphicsScene matrix of other QGraphicsScene
    By Noxxik in forum Qt Programming
    Replies: 5
    Last Post: 15th February 2009, 17:27
  4. Help on QGraphicsScene
    By Sandip in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2008, 14:20
  5. Question about QImages
    By SkripT in forum Qt Programming
    Replies: 14
    Last Post: 27th January 2006, 11:45

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.