Results 1 to 3 of 3

Thread: Multi threading ...

  1. #1
    Join Date
    Dec 2006
    Posts
    17
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Multi threading ...

    Hi ,

    I have a problem with freezing the display....

    Problem is similar to this model.

    step 1. I need to create and display thousands of rectangles. ( QGraphicsItem)
    step 2. Do some calculation and Create lines ( by default hidden ) b/w the rectangles. ( millions of lines)
    Here lines and rects are QGraphicsItems

    Creation and display of rectangles is fast but creation of lines is taking too much time.

    So iam planning to multithread the application, by executing the creation of lines in seperate thread to prevent freezing the display.

    After displaying the Rectangles. The user should be able to do Zoom In, Zoomout, Panning.
    While the background thread is ceating the lines.

    My problem is ....
    1. Is it possible to do this ? since Both the threads will be accessing the same GraphicsScene.
    Main thread will be reading other thread will be adding line items to it.
    2. Is there any way to synchronize both the threads ?
    3. Is there any other solution?

    Thanks...

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Multi threading ...

    Hi,

    The working thread can't draw directly to the screen. Every time it has a calculated line, it have to emit it to the main thread and the main thread has to display the line.

    Take a look at "mandelbrot" example. It has a worker thread that render(calculate) an image and send it to the main thread that will display it.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Multi threading ...

    Quote Originally Posted by kiranraj View Post
    1. Is it possible to do this ?
    Yes.
    since Both the threads will be accessing the same GraphicsScene.
    Use signals and slots - do all needed calculations in the worker thread and then signal the results to the main thread that will insert the new item into the scene.
    Main thread will be reading other thread will be adding line items to it.
    No, items have to be added by the main thread. If you don't need any calculations and just want to add a bunch of items without freezing the application then you won't need another thread - just use a timer with a 0 timeout.
    2. Is there any way to synchronize both the threads ?
    Signals and slots or events (which is practically the same).
    3. Is there any other solution?
    I think I already answered that... But anyway a small snippet of code to illustrate:
    Qt Code:
    1. class GraphicsScene : public QGraphicsScene {
    2. Q_OBJECT
    3. public:
    4. GraphicsScene(...) : QGraphicsScene(...){
    5. m_last = -1;
    6. initializeScene(); // starts the asynchronous task that fills the scene with items
    7. }
    8. private slots:
    9. void initializeScene(){
    10. int done = 0;
    11. while(++m_last<10000 && done++<10){
    12. addLine(...);
    13. }
    14. if(m_last>=10000)
    15. return;
    16. QTimer::singleShot(0, this, SLOT(initializeScene()));
    17. }
    18. };
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Threading and plotting graph in same program.
    By sar_van81 in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 9th May 2007, 20:42
  2. multi screen
    By Thomas Feldman in forum Qt Programming
    Replies: 5
    Last Post: 9th May 2007, 16:41
  3. Newbie threading question
    By deepayan in forum Qt Programming
    Replies: 17
    Last Post: 16th April 2007, 00:25
  4. multi pixmap transparency
    By bluesguy82 in forum Qt Programming
    Replies: 3
    Last Post: 21st August 2006, 22:13
  5. Multi frame management ... is it possible ?
    By yellowmat in forum Newbie
    Replies: 8
    Last Post: 25th January 2006, 10:41

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.