Results 1 to 7 of 7

Thread: X process 99% after 2 hours using drawPixmap

Hybrid View

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

    Default X process 99% after 2 hours using drawPixmap

    Hy all,
    i've a simple code to draw a rotating ship on a background image. Every second a receive a message with the new parameters to update the draw,
    and in my main method i
    call

    ...
    QWidget::update();
    drawShip(); }


    Problem is that after 2 hours of activity all the system become very slow and i noticed that X process is at 99%
    Is there any bug in my code ?


    This is my simple code :

    void Board::drawShip()
    {
    int X_Background_PPI;
    int Y_Background_PPI;


    int PPI_SIZE = 220; // is a square
    int CIRCLE_OFFSET = 6; // starting point for Circle PPI

    X_Background_PPI = 10 +7;
    Y_Background_PPI = 358 +7;


    X_Nave_PPI = X_Background_PPI + PPI_SIZE/2;
    Y_Nave_PPI = Y_Background_PPI + PPI_SIZE/2 ;


    QPen Pen (Qt::yellow, 4, Qt::SolidLine);
    QBrush Brush (Qt::black, Qt::SolidPattern);

    QPainter painter(this);
    QPainter painter1(this);
    QPainter painter2(this);
    QPainter painter3(this);

    painter.setRenderHint(QPainter::Antialiasing, true);
    painter1.setRenderHint(QPainter::Antialiasing, true);

    painter.drawPixmap(X_Background_PPI,Y_Background_P PI, Background_PPI); // draw the bacground


    // draw the ship shape
    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, Nave_PPI );
    painter1.restore(); // // restore the previous painter settings

    int diameter = PPI_SIZE - 2 * CIRCLE_OFFSET ;
    int diameter1 = diameter - 80 ;
    int diameter3 = diameter - 180 ;

    int Wx, Wy = 0;
    int Wx1, Wy1 = 0;

    float New_Wind_rad;

    New_Wind_rad = New_Wind * grad2rad;

    Wx = X_Nave_PPI + (diameter/2 * sin(New_Wind_rad) ); // in radiants !!! warning int and float
    Wy = Y_Nave_PPI - (diameter/2 * cos(New_Wind_rad) ); // in radiants !!! warning int and float

    Wx1 = X_Nave_PPI + (diameter1/2 * sin(New_Wind_rad) ); // in radiants !!! warning int and float
    Wy1 = Y_Nave_PPI - (diameter1/2 * cos(New_Wind_rad) ); // in radiants !!! warning int and float


    // draw the wind vector
    painter1.save(); // save the current printer settings before changing them
    painter1.translate(Wx,Wy); // // the point about which the image rotates
    painter1.rotate(New_Wind); //degrees;
    painter1.drawPixmap(- Wind_Arrow.width()/2,0, Wind_Arrow );
    painter1.restore(); // // restore the previous painter settings
    }

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: X process 99% after 2 hours using drawPixmap

    Take a look at this warning in Qt Docs
    Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent widget attribute is set. On Mac OS X and Windows, you can only paint in a paintEvent() function regardless of this attribute's setting.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    Default Re: X process 99% after 2 hours using drawPixmap

    Yes, i do not post all the code.. but i call the qpaintevent in my :


    void drawShip()
    {
    int X_Background_PPI;
    int Y_Background_PPI;



    int PPI_SIZE = 220; // is a square
    int CIRCLE_OFFSET = 6; // starting point for Circle PPI

    MyFramework:: paintEvent(event);

    ....


    All seems to work well, i see the ship rotating according to the received data, problem is that after 2 hours the system become very slow and i noticed that X process is at 99%

    Am i missing something ?? What's wrong with my code ?

    Thanks Gabriele

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: X process 99% after 2 hours using drawPixmap

    QPainter can only be used inside a paintEvent() function or in a function called by paintEvent();
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    Default Re: X process 99% after 2 hours using drawPixmap

    OK...sorry for my ignorance...but i have 2 question to better understand :

    1) Why my code works, even if i use a simply call to qpaintevent implemented in the framework that i use ?
    2) I've to reimplement qpaintevent function in my Widget code ? is it right ?

    Thanks in advance for your precious support.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: X process 99% after 2 hours using drawPixmap

    1) Why my code works, even if i use a simply call to qpaintevent implemented in the framework that i use ?
    What documentation says is not to use QPainter outside paintEvent(), it does not explicitly not mention why? May be some kind of design constraint, or may be some feature will not work outside paintEvent().

    I know it works, I too used it some time back. I was painting a kind of water mark on the all the widgets using event filters. We can never be sure when it will break. So better not use in a way which is explicitly mention not to.

    2) I've to reimplement qpaintevent function in my Widget code ? is it right ?
    Yes, and simply call drawShip() from PaintEvent.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    Default Re: X process 99% after 2 hours using drawPixmap

    Ok so i do this implementation :

    in .h i add

    void paintEvent(QPaintEvent *event);

    in my .cpp i write :

    void MyCass :: paintEvent(QPaintEvent *event)
    {
    drawShip();

    }

    and i call

    QWidget::update(); // to update paint event

    when i receive the message to update the ship.
    Is this right ??

    Thanks Gabriele

Similar Threads

  1. How do i get the number of hours
    By ayanda83 in forum Newbie
    Replies: 2
    Last Post: 4th January 2013, 10:21
  2. drawPixmap() ...problem or bug or......??
    By Programm3r in forum Qt Programming
    Replies: 0
    Last Post: 21st April 2010, 09:28
  3. Date and hours
    By kjiu in forum Qt Programming
    Replies: 9
    Last Post: 13th November 2009, 18:27
  4. QPainter::drawPixmap with floats ?
    By christophe.daudin in forum Qt Programming
    Replies: 8
    Last Post: 20th October 2009, 10:19
  5. can we use drawPixmap() with svgfiles?
    By sanjayshelke in forum Qt Programming
    Replies: 1
    Last Post: 24th July 2009, 12:28

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.