Results 1 to 13 of 13

Thread: Strange behaviour with QPainter

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Strange behaviour with QPainter

    Hi to all, I can not update the position of a moving triangle in my paintEvent. It stay at the initial position.
    This little triangle is used to simulate a time position so it must move indicating time that move when a sound is playing.

    Some code:

    Qt Code:
    1. // here I determine the current time position ( where I would place the triangle
    2. void WaveWidget::setCurrentTimePosition()
    3. {
    4. uint newTimeValue = getCurrentPosInSoundCoord();
    5.  
    6. /* trasform the sound position in widget coordinate */
    7. qreal samplesPerPixel = m_wave->getSamples() / width();
    8. qreal x_pos = newTimeValue / samplesPerPixel;
    9.  
    10. if( x_pos == m_CurrentTimePosition )
    11. return; //no change
    12.  
    13. // I inform that time-position is changed
    14. emit soundPosChanged( m_CurrentTimePosition, x_pos );
    15.  
    16. /* update current position */
    17. m_CurrentTimePosition = x_pos;
    18. }
    To copy to clipboard, switch view to plain text mode 

    The code of the slot that's informed of the time changes is:
    Qt Code:
    1. void TimeWidget::soundPosChanged( qreal currentTimePosition, qreal newTimePosition )
    2. {
    3. int w = width();
    4. int h = height();
    5.  
    6. m_CurrentTimePosition = currentTimePosition;
    7.  
    8. unsigned int rectWidth = ( newTimePosition - m_CurrentTimePosition ) + 4;
    9. //qDebug() << rectWidth;
    10.  
    11. // determine the rect to update
    12. //m_CurrentTimePosition is the triangle central point
    13. QRect r = QRect( m_CurrentTimePosition - 3, h/2 + 1, rectWidth, h/2 );
    14.  
    15. // update it
    16. update(r);
    17. // update current position
    18. m_CurrentTimePosition = newTimePosition;
    19. }
    To copy to clipboard, switch view to plain text mode 

    Now the code of the paintEvent

    Qt Code:
    1. void TimeWidget::paintEvent( QPaintEvent* e)
    2. {
    3. int h = height();
    4. int w = width();
    5.  
    6. QPainter p( this );
    7. p.setRenderHint( QPainter::Antialiasing, true );
    8. p.fillRect( 0, 0, w, h, Qt::lightGray );
    9. p.setBrush(Qt::blue);
    10.  
    11. static const QPointF points[3] =
    12. {
    13. QPointF( m_CurrentTimePosition -2, h/2),
    14. QPointF( m_CurrentTimePosition + 2, h/2),
    15. QPointF( m_CurrentTimePosition, h)
    16. };
    17.  
    18. qDebug() << "TimeWidget::paintEvent - centralPoint:" << m_CurrentTimePosition;
    19.  
    20. p.drawPolygon( points, 3 );
    21. }
    To copy to clipboard, switch view to plain text mode 

    I don't know why the triangle doesn't move.
    The centralPoint m_CurrentTimePosition is correctly updated as I can see it at shell.

    I really don't know where my code is wrong.

    I tried to add the following line:
    Qt Code:
    1. QRect region = e->rect();
    To copy to clipboard, switch view to plain text mode 

    as first line in the paintEvent routine and I got the following compiiler error:

    error C2027: use of undefined type 'QPaintEvent'
    I'm really confused.

    Best Regards
    Last edited by franco.amato; 13th January 2010 at 21:17.
    Franco Amato

  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: Strange behaviour with QPainter

    I would get rid of the slot and call update() from within the method where you change the value of your property. Calling update() with a parameter doesn't make much sense as you don't use the region in your paintEvent() anyway. As for the error - #include <QPaintEvent>
    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
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange behaviour with QPainter

    Quote Originally Posted by wysota View Post
    I would get rid of the slot and call update() from within the method where you change the value of your property. Calling update() with a parameter doesn't make much sense as you don't use the region in your paintEvent() anyway. As for the error - #include <QPaintEvent>
    OK error solved,
    but I changed update( r ) with update and nothing change.
    I can not draw the triangle. It stay at position 0.

    Maybe a bug of drawPolygon?
    Franco Amato

  4. #4
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange behaviour with QPainter

    I think is a drawPolygon problem.

    I tried to leave update (r) in the slot and changed

    p.drawPolygon( points, 3 );

    with

    p.drawRect(e->rect()); only for testing and now I can see something moving in the widget so I don't know how to solve it.
    And other I think is better call update (r) instead of only update() so only a little portion of widget is updated.

    This sould increase speed or am I wrong?

    Best
    Franco Amato

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Strange behaviour with QPainter

    Just guessing but what if you get rid of "static" before const QPointF points[3]?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #6
    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: Strange behaviour with QPainter

    It will magically start working


    @franco.amato: Please look for errors in your own code instead of blaming others.
    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. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange behaviour with QPainter

    Quote Originally Posted by faldżip View Post
    Just guessing but what if you get rid of "static" before const QPointF points[3]?
    Yesssssssssssssssssssssssss it solved my problem.


    Best
    Franco Amato

  8. #8
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Strange behaviour with QPainter

    So my advice is to read this carefully
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  9. #9
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange behaviour with QPainter

    Quote Originally Posted by faldżip View Post
    So my advice is to read this carefully
    I still didn't understand why the static gave that behaviour if I update at every call the member variablke
    Franco Amato

  10. #10
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange behaviour with QPainter

    Quote Originally Posted by wysota View Post
    It will magically start working


    @franco.amato: Please look for errors in your own code instead of blaming others.
    Sorry what are you speaking about?
    Franco Amato

  11. #11
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Strange behaviour with QPainter

    Quote Originally Posted by franco.amato View Post
    I still didn't understand why the static gave that behaviour if I update at every call the member variablke
    Here is a small example:
    Qt Code:
    1. void foo() {
    2. static int counter = 0;
    3. cout << "foo has been called " << ++counter << " times\n";
    4. }
    5.  
    6. int main() {
    7. for( int i = 0; i < 10; ++i ) foo();
    8. }
    To copy to clipboard, switch view to plain text mode 
    it will display:
    foo has been called 1 times
    foo has been called 2 times
    foo has been called 3 times
    ...
    foo has been called 10 times

    So, as it can be easily noticed, this line:
    Qt Code:
    1. static int counter = 0;
    To copy to clipboard, switch view to plain text mode 
    is called ONCE, when this line is rached for the first time. So counter variable is created once and 0 is assigned to it once. Then it remains in memory for every foo() call until the end of app execution.
    So in your code point[3] was created and initialized once on the first call of paintEvent() and remained there forever (until program ends).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  12. #12
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange behaviour with QPainter

    Quote Originally Posted by faldżip View Post
    Here is a small example:
    Qt Code:
    1. void foo() {
    2. static int counter = 0;
    3. cout << "foo has been called " << ++counter << " times\n";
    4. }
    5.  
    6. int main() {
    7. for( int i = 0; i < 10; ++i ) foo();
    8. }
    To copy to clipboard, switch view to plain text mode 
    it will display:
    foo has been called 1 times
    foo has been called 2 times
    foo has been called 3 times
    ...
    foo has been called 10 times

    So, as it can be easily noticed, this line:
    Qt Code:
    1. static int counter = 0;
    To copy to clipboard, switch view to plain text mode 
    is called ONCE, when this line is rached for the first time. So counter variable is created once and 0 is assigned to it once. Then it remains in memory for every foo() call until the end of app execution.
    So in your code point[3] was created and initialized once on the first call of paintEvent() and remained there forever (until program ends).
    OK It's all clear.
    Thank you again
    Franco Amato

  13. #13
    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: Strange behaviour with QPainter

    Quote Originally Posted by franco.amato View Post
    Sorry what are you speaking about?
    Don't you think that if drawPolygon() wasn't working someone would notice it like... a few years ago?
    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. QMainWindow - strange behaviour
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2009, 23:44
  2. Need help: Strange behaviour
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2008, 04:03
  3. Strange behaviour of QPainter...
    By oscar in forum Qt Programming
    Replies: 2
    Last Post: 8th November 2008, 12:07
  4. Strange Painter behaviour
    By cwomeijer in forum Qt Programming
    Replies: 2
    Last Post: 4th September 2006, 20:52
  5. very strange behaviour
    By regix in forum Qt Programming
    Replies: 23
    Last Post: 20th July 2006, 17:38

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.