Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: QImage and Qt 4.5.3

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

    Default QImage and Qt 4.5.3

    Hi;
    I developped an application drawing images using QPainter.
    First on OpenSUSE 11.0 with Qt 4.4.0, and it WORKS and print pictures, but have warnings for each picture I print.
    Qt Code:
    1. QPixmap: It is not safe to use pixmaps outside the GUI thread
    2. X Error: RenderBadGlyphSet (invalid GlyphSet parameter) 160
    3. Extension: 148 (RENDER)
    4. Minor opcode: 25 (RenderCompositeGlyphs32)
    5. Resource id: 0x0
    To copy to clipboard, switch view to plain text mode 

    Second on Ubuntu 9.10 with Qt 4.5.3 it diden't work, and didnt print anything with the same warnings.

    Here is the code of paintEvent methode of the Widget I want to paint.

    Qt Code:
    1. void paintEvent(QPaintEvent *event){
    2.  
    3. const QPoint point(100,100);
    4. QImage image(pictureBuffer, width, height, QImage::QImage::Format_RGB888);
    5. QPainter painter;
    6. painter.begin(this);
    7. painter.drawImage(point,image);
    8. painter.end();
    9. }
    To copy to clipboard, switch view to plain text mode 

    Thank you for helping.

  2. #2
    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

    What is pictureBuffer? What does it contain? How is it populated?
    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. #3
    Join Date
    Feb 2010
    Posts
    19
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage and Qt 4.5.3

    pictureBuffer is an unsigned char array containing RGB pixels.
    in OpenSUSE + Qt 4.4, it displays the content of the buffer with no problem.
    The display is done by another Thread, I ll try to draw with the main thread to see

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

    Default Re: QImage and Qt 4.5.3

    I tried to Draw with the main thread an it works. But it didnt match what I want.
    This application is a video player.
    I Have a main thread who creates a mainwidow where I have a Panel to display the Video and Buttons (Play, Pause,...)
    Then I create 2 threads; one to make the pictures to the other who displays them.
    So, If the work done by the second thread is done by the main thread, it works but I cant Access the buttons.
    Thats the probleme
    thank you for any suggestion

  5. #5
    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

    What do you mean exactly by drawing in the other thread? What are you drawing there? The widget?
    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.


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

    Default Re: QImage and Qt 4.5.3

    Yes
    The main window contains the widget where I draw the pictures. This widget implements the paintEvent methode like I say it first time.
    I think the main thread (the GUI thread) is the only thread allowed to repaint the widgets.
    Can we send signals between threads.
    I ll try to modify the content of the buffer with The second thread , emit signal to the main thread to repaint.

  7. #7
    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
    Yes
    The main window contains the widget where I draw the pictures. This widget implements the paintEvent methode like I say it first time.
    Ok but how do you update it in another thread? Could we see the code for that?

    I think the main thread (the GUI thread) is the only thread allowed to repaint the widgets.
    Yes, that's correct.
    Can we send signals between threads.
    Yes, we can.
    I ll try to modify the content of the buffer with The second thread , emit signal to the main thread to repaint.[/SIZE]
    That's a good approach.
    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.


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

    Default Re: QImage and Qt 4.5.3

    The code of the thread is lot of calculations, but we can resume it in :
    Qt Code:
    1. while(!EOS)
    2. {
    3. actual_picture = this->Decoder->getNextPicture();
    4. this->Picture->repaint(); // to be changed into : emit draw()
    5. }
    To copy to clipboard, switch view to plain text mode 
    Picture is the widget drawing the pictures.
    Decoder : global variable decoding a stream. EOS: end of stream
    actual_picture is a global variable: the actual picture to be displayed.

    I tried to use signals like I said. I added the signal draw(), and the slot drawPic():

    Qt Code:
    1. void drawPic(){
    2. this->Picture->repaint();
    3. }
    To copy to clipboard, switch view to plain text mode 

    But this solution is not correct :
    The second thread gets pictures and modifies actual_picture many times before the signal is handeled by the GUI thread.
    So we have less pictures displayed than decoded.

  9. #9
    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

    It's enough to do update() instead of repaint() and painting will be done in the main thread. You don't need any signals or anything for that. But be aware that you are accessing the same variable (the image) from two threads, you need to do proper thread synchronization (or transfer the image between threads using events or signals which will provide synchronization for you at the expense of some overhead).
    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.


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

    Default Re: QImage and Qt 4.5.3

    Ok, I used the signals to transfer the images between the threads
    But the number of signals handeled is still < to the emitted.
    I thought that signals are stored in some of queue and they'll be all handeled. am I wrong?

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

    Default Re: QImage and Qt 4.5.3

    How can I wait, in the second thread, the termination of the display in the GUI thread for each picture.

  12. #12
    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
    But the number of signals handeled is still < to the emitted.
    Maybe you are generating them faster than they can be processed.
    I thought that signals are stored in some of queue and they'll be all handeled. am I wrong?
    They are.
    Quote Originally Posted by halmassi View Post
    How can I wait, in the second thread, the termination of the display in the GUI thread for each picture.
    There are many ways, for instance you can use a wait condition (QWaitCondition) to wait on a mutex. But what exactly is the point of doing that?
    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.


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

    Smile Re: QImage and Qt 4.5.3

    Maybe you are generating them faster than they can be processed.
    Yes. thats correct.

    But what exactly is the point of doing that?
    Because I generate pictures faster than I display them, I want to
    generate signal with the picture
    wait until the GUI thread displays the picture.
    generate next signal with picture.

    I dont know if it is the best way to solve the probleme

  14. #14
    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

    It's ok. Use a wait condition to protect the shared resource. You don't even need signals at all.
    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.


  15. The following user says thank you to wysota for this useful post:

    halmassi (10th March 2010)

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

    Default Re: QImage and Qt 4.5.3

    Its OK,
    but I used a mutex instead of waitcondition, and I kept my signals.

    Second THREAD:
    -- get picture
    -- send signal with the picture
    -- block on mutex

    GUI Thread Slot:
    -- display picture
    -- wake up 2nd thread

    thanks wysota

  17. #16
    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

    If that's all you did then it's kind of wrong. You should wake the other thread before you display the picture and you should do that only if the picture to display changed since the last time you displayed it. Otherwise you might be releasing an already released mutex or blocking the other thread unnecessary.
    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.


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

    Default Re: QImage and Qt 4.5.3

    No it works. Perhaps I didnt explain very well.
    The GUI thread displays the picture only if he had received a signal from the other thread.
    receiving the signal means two things: 1) the picture had been changed by the other thread, 2) the thread sending the signal is blocked on the Mutex
    So when the GUI thread finishes displaying he woke up the other thread.

  19. #18
    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
    No it works. Perhaps I didnt explain very well.
    The fact that it works for situations you test it against doesn't mean it will work in every conditions.

    The GUI thread displays the picture only if he had received a signal from the other thread.
    What if you resize the widget or obscure it with another window? It will get paint events. Won't it interfere with your mechanism?
    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.


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

    Default Re: QImage and Qt 4.5.3

    The mutex mecanism is in the slot drawPic() witch works only with the signal draw();
    So, other painting events causes repainting widget using paintEvent(); and will repaint the last picture
    and no mutex is touched. We cant release a mutex already released and same with locking it.
    I ll test these conditions to know the results.

  21. #20
    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

    Can you show us the code from both threads that accesses the mutex?

    Here is a mutex-free implementation, by the way:
    Qt Code:
    1. connect(worker, SIGNAL(newImage(QImage)), widget, SLOT(drawPic(QImage)), Qt::BlockingQueuedConnection);
    2. //...
    3. void Widget::drawPic(QImage img){
    4. m_image = img;
    5. update();
    6. }
    7.  
    8. void Widget::paintEvent(QPaintEvent *pe){
    9. QPainter p(this);
    10. p.drawImage(m_image, ...);
    11. }
    To copy to clipboard, switch view to plain text mode 
    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.


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.