Results 1 to 4 of 4

Thread: drawPixmap - image disappears

  1. #1
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default drawPixmap - image disappears

    Good morning,
    i've to draw an arrow on a widget. so i have the xpm file for the arrow.
    I draw the arrow only if the new angle is more than 5 degree.
    Problem is that on my widget i see the arrow, then it disappears and come back only when the drawship metod is call again, then disappears again..and so on..as flickering...

    I thought that when drawpxmap draws the image it remains on the widget untill a new drawpixmap will be called...

    What is my error ?

    This is the simple code :



    void Class:: paintEvent(QPaintEvent *event)
    {


    if(fabs(New_Heading - OSHeading) > 5 )
    {

    drawShip();
    }
    }


    void Class::drawShip()
    {


    QRegion myregion1(X_Background_PPI, Y_Background_PPI, PPI_SIZE, PPI_SIZE);

    QPainter painter1(this);
    painter1.setClipRegion(myregion1);
    painter1.setRenderHint(QPainter::Antialiasing, true);

    painter1.save(); // save the current printer settings before changing them
    painter1.translate(X_Nave_PPI,Y_Nave_PPI); // // the point about which the image rotates
    painter1.rotate(New_Heading); //degrees;
    painter1.drawPixmap(-Nave_PPI.width()/2, - Nave_PPI.height()/2, ARROW_PPI );
    painter1.restore(); // // restore the previous painter settings
    }

  2. #2
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawPixmap - image disappears

    I thought that when drawpxmap draws the image it remains on the widget untill a new drawpixmap will be called...
    No, every time the widget is repainted (it can happen for a number of reasons like resizing, minimizing/restoring window, etc...) it is cleared of all content and paintEvent called.
    So you need to draw all widget contents each time paintEvent occurs.
    In your case you draw it once, then when another paintEvent is fired, your code takes else branch and draws nothing, so the widget stays empty.
    If you concerned about performance (which is maybe prematurely optimization in your case) you can draw your scene to backbuffer pixmap and then draw only that pixmap in paintEvent.
    Also you can always switch to OpenGL.

  3. #3
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Re: drawPixmap - image disappears

    OK thankyou !!
    One question :

    You say i've to copy my pixmap to a backbuffer pixmap...
    PLease can you tell me how do this ?
    Consider that in my drawship method i have 2
    drawpixmap call: one to draw an arrow and another to draw a ship.

    painter1.drawPixmap(-Nave_PPI.width()/2, - Nave_PPI.height()/2, ARROW_PPI );
    painter1.drawPixmap(-Nave_PPI.width()/2, - Nave_PPI.height()/2, SHIP_PPI );

    How can i create a backbuffer pixmap containing the pixmap generated by the 2 commands ?

    Thanks Gabriele

  4. #4
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawPixmap - image disappears

    You can use pixmap as paint device:
    Qt Code:
    1. class Class {
    2. QPixmap *backbuffer;
    3. };
    4. void Class::drawShip () {
    5. QPainter back_painter (this->backbuffer);
    6. // drawing code
    7. };
    8. void Class::paintEvent(QPaintEvent *event) {
    9. QPainter painter1(this);
    10. painter1.drawPixmap (0,0, this->backbuffer);
    11. };
    To copy to clipboard, switch view to plain text mode 
    Of course you need to initialize pixmap, and keep it at actual size(e.g. after resize events) and valid state (after ship rotated or moved).

Similar Threads

  1. X process 99% after 2 hours using drawPixmap
    By gab74 in forum Qt Programming
    Replies: 6
    Last Post: 24th January 2013, 18:02
  2. drawPixmap() ...problem or bug or......??
    By Programm3r in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2010, 09:28
  3. QPainter::drawPixmap with floats ?
    By christophe.daudin in forum Qt Programming
    Replies: 8
    Last Post: 20th October 2009, 10:19
  4. can we use drawPixmap() with svgfiles?
    By sanjayshelke in forum Qt Programming
    Replies: 1
    Last Post: 24th July 2009, 12:28
  5. a runtime errors in QPainter::DrawPixmap(..)
    By richardander in forum Qt Programming
    Replies: 1
    Last Post: 12th February 2009, 17:46

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
  •  
Qt is a trademark of The Qt Company.