Results 1 to 13 of 13

Thread: Strange behaviour with QPainter

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    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

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