Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27

Thread: QImage and Qt 4.5.3

  1. #21
    Join Date
    Feb 2010
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage and Qt 4.5.3

    Qt Code:
    1. /*initialisation*/
    2. pthread_mutex_t Mutex;
    3. pthread_mutex_init(&Mutex,NULL);
    4. pthread_mutex_lock(&Mutex); // init to 0
    5.  
    6. connect(worker, SIGNAL(newImage(QImage)), widget, SLOT(drawPic(QImage)), Qt::BlockingQueuedConnection);
    7. //...
    8. void Widget::drawPic(QImage img){
    9. m_image = img;
    10. update();
    11. pthread_mutex_unlock(&Mutex);
    12. }
    13.  
    14. void Widget::paintEvent(QPaintEvent *pe){
    15. QPainter p(this);
    16. p.drawImage(m_image, ...);
    17. }
    18.  
    19. void worker:: play(){
    20. while(condition){
    21. QImage img = decoder->getImage();
    22. emit newImage(img);
    23. pthread_mutex_lock(&Mutex); //block until waked by widget
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #22
    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: QImage and Qt 4.5.3

    Is that your code or mine? BTW. There is QMutex, you know. And if you are using a blocking queued connection the mutex is not required (provided that "worker" lives in the worker thread). Either way this solution is inefficient - your worker threads will continue to produce images regardless of the widget being able to refresh itself. update() schedules a repaint and if two update() calls occur before the repaint is actually done, they are merged into one. So it is likely to happen that you will produce 10 images but only display the last one.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #23
    Join Date
    Feb 2010
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage and Qt 4.5.3

    I used your code to resume mine.

    I prefer use the Posix threads and mutexes.

    How can we use a blocking queued connection.

    The worker cant produce several images without displaying them all.
    Because, After the first image he will block on the mutex until Widget wake him

  4. #24
    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: QImage and Qt 4.5.3

    Quote Originally Posted by halmassi View Post
    I used your code to resume mine. :)

    I prefer use the Posix threads and mutexes.
    That's what Qt uses too but it gives you a nicer interface.

    How can we use a blocking queued connection.
    If you are not using QThread then you can't.

    The worker cant produce several images without displaying them all.
    Because, After the first image he will block on the mutex until Widget wake him
    No, that's not true. This is because the worker thread is woken up directly after a call to update(). And a call to update() doesn't cause the widget to be painted. So the following sequence of events may happen:

    worker::newImage()
    widget::drawPic()
    widget::update()
    worker::newImage()
    widget::drawPic()
    widget::update()
    worker::newImage()
    widget::drawPic()
    widget::update()
    worker::newImage()
    widget::drawPic()
    widget::update()
    worker::newImage()
    widget::drawPic()
    widget::update()
    widget::paintEvent() <- here the widget is repainted

    Especially if you're not using QThread...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #25
    Join Date
    Feb 2010
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage and Qt 4.5.3

    Ok thanks, You are Right.
    Now I understand.
    so you think, it will be better to switch to QThread.
    I have another aspect of the application, where I create several Threads (the number of threads is known at the execution)
    dynamicaly but using the same function:
    How can I do that with QThread.

  6. #26
    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: QImage and Qt 4.5.3

    You can subclass QThread and reimplement its run() method. Remember to push the QThread object into its own thread using QObject::moveToThread() (search the forum for details on what it does and why it's needed). You can then use blocking queued connection to obtain the same effect you have now or use a mutex and optionally a wait condition to make sure the worker thread is activated only after the image is really painted.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #27
    Join Date
    Feb 2010
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage and Qt 4.5.3

    Ok, I ll spend time to switch to Qthread.
    We close this thread, and I ll be back in few days.
    Thanks for all wysota.

Similar Threads

  1. QImage
    By hazardpeter in forum Newbie
    Replies: 2
    Last Post: 31st July 2009, 15:19
  2. QImage
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 17th October 2007, 20:35
  3. What's faster: QPixmap-to-QImage or QImage-to-QPixmap
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 15th December 2006, 17:11
  4. QImage
    By mickey in forum Qt Programming
    Replies: 7
    Last Post: 14th July 2006, 21:54
  5. Replies: 3
    Last Post: 15th March 2006, 11:44

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.