Results 1 to 9 of 9

Thread: How to update a graphics item immediately??

  1. #1
    Join Date
    Mar 2015
    Posts
    8
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default How to update a graphics item immediately??

    I am making a list of blue rectangles and then try to change color of them one by one to the end of the list. Here is my source code:

    traverse() in dialog.cpp
    Qt Code:
    1. void Dialog::traverse()
    2. {
    3. Node *cur = head;
    4. while (cur)
    5. {
    6. colorAnimation = new QPropertyAnimation(cur->square, "color");
    7. colorAnimation->setDuration(4000);
    8. colorAnimation->setKeyValueAt(0.0, RED);
    9. colorAnimation->setKeyValueAt(0.9, RED);
    10. colorAnimation->setKeyValueAt(1.0, CYAN);
    11. colorAnimation->start();
    12.  
    13. clock_t waiter = clock() + 4 * CLOCKS_PER_SEC;
    14. while (clock() < waiter);
    15. cur = cur->next;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    square.h
    Qt Code:
    1. const int CYAN = 0;
    2. const int RED = 1;
    3. class Square : public QObject, public QGraphicsItem
    4. {
    5. Q_OBJECT
    6. Q_PROPERTY(int color MEMBER color WRITE setColor)
    7. public:
    8. Square();
    9. Square(int n);
    10. ~Square();
    11. QRectF boundingRect() const;
    12. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    13. void setColor(int newColor);
    14. private:
    15. int color;
    16. int order;
    17. };
    To copy to clipboard, switch view to plain text mode 

    square.cpp
    Qt Code:
    1. #include "square.h"
    2.  
    3. Square::Square()
    4. {
    5. color = CYAN;
    6. order = 0;
    7. }
    8.  
    9. Square::Square(int n)
    10. {
    11. color = CYAN;
    12. order = n;
    13. }
    14.  
    15. Square::~Square()
    16. {
    17. }
    18.  
    19. QRectF Square::boundingRect() const
    20. {
    21. return QRectF(X_START + order*HDISTANCE, Y_START, B_WIDTH, HEIGHT);
    22. }
    23.  
    24. void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    25. {
    26. QRectF rect = QRectF(X_START + order*HDISTANCE + LINE, Y_START, WIDTH, HEIGHT);
    27. QPen pen(Qt::blue);
    28. pen.setCapStyle(Qt::RoundCap);
    29. QBrush brush(Qt::cyan);
    30.  
    31. if (color == RED)
    32. {
    33. brush.setColor(Qt::red);
    34. }
    35. else if (color == CYAN)
    36. {
    37. brush.setColor(Qt::cyan);
    38. }
    39.  
    40. painter->fillRect(rect, brush);
    41. painter->drawRect(rect);
    42. }
    43.  
    44. void Square::setColor(int newColor)
    45. {
    46. color = newColor;
    47. update();
    48. }
    To copy to clipboard, switch view to plain text mode 

    It happens to be a group of rectangles changing to red at the same time. I think it is because the update() function doesn't repaint the item immediately, but I can not use the repaint() function to modify a graphics item. Can anyone suggest a way to repaint the item immediately? Thanks you very much.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to update a graphics item immediately??

    Qt Code:
    1. colorAnimation->setDuration(4000);
    2. colorAnimation->setKeyValueAt(0.0, RED);
    3. colorAnimation->setKeyValueAt(0.9, RED);
    4. colorAnimation->setKeyValueAt(1.0, CYAN);
    To copy to clipboard, switch view to plain text mode 
    I'm wondering what are you trying to visualize, as your "animation" is a just a single color 99% of the time.
    What's wrong with simple "setColor" call instead of creating a (probably leaking) animation object each time ?
    Btw. don't use busy waiting (the "while" hack) as it is almost never needed in Qt applications.

    If I understand your problem correctly, you are trying to change the color of each item one by one with some kind of delay between each change. If this is correct, then maybe try to use a QTimer with a proper interval and simply call "setColor" on the rectangles one after another.

  3. The following user says thank you to stampede for this useful post:

    Rabbitl96l (3rd April 2015)

  4. #3
    Join Date
    Mar 2015
    Posts
    8
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to update a graphics item immediately??

    I have tried to use QTimer, but the rectangles still change color at the same time because of the update( ) function.

    Qt Code:
    1. void Dialog::traverse()
    2. {
    3. Node *cur = list.getHead();
    4. timer = new QTimer(this);
    5. while (cur)
    6. {
    7. connect(timer, SIGNAL(timeout()), cur->square, SLOT(changeColor()));
    8. timer->start(3000);
    9. cur = cur->next;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    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: How to update a graphics item immediately??

    Lose the while loop. Start the timer and in the timeout slot modify one item. Next time timeout is invoked modify the next item and so on.
    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. The following user says thank you to wysota for this useful post:

    Rabbitl96l (4th April 2015)

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to update a graphics item immediately??

    Lose the while loop
    I think the OP's idea is to change the colors of all of the squares "simultaneously" (as best as that can be accomplished). That's why the multiple timers / animations and the while loop.

    From my understanding of QPropertyAnimation, the external QTimer is completely unnecessary - the animation does its own internal timing. It is most likely this code:

    Qt Code:
    1. clock_t waiter = clock() + 4 * CLOCKS_PER_SEC;
    2. while (clock() < waiter);
    To copy to clipboard, switch view to plain text mode 

    and the fact that the event look is never executed during that time which results in the failure to update the colors as planned. But there isn't enough context in the source code that is shown to figure out what the real problem is.

  8. The following user says thank you to d_stranz for this useful post:

    Rabbitl96l (4th April 2015)

  9. #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: How to update a graphics item immediately??

    Quote Originally Posted by d_stranz View Post
    I think the OP's idea is to change the colors of all of the squares "simultaneously" (as best as that can be accomplished).
    I don't know. He said it was incorrect behavior that they did change all at once:

    Quote Originally Posted by Rabbitl96l
    the rectangles still change color at the same time
    Quote Originally Posted by d_stranz
    That's why the multiple timers / animations and the while loop.
    I think he thought starting a timer would synchronously delay execution.

    But there isn't enough context in the source code that is shown to figure out what the real problem is.
    I agree
    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. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to update a graphics item immediately??

    but the rectangles still change color at the same time
    If I read between the lines, I think he means that the animation isn't occurring - the color just goes from blue to red without the fade in. At least that is my interpretation, and the code he did post would result in that behavior, I think.

    Am I correct in assuming that time 0 is established when the animation's start() slot is called, and that if his code doesn't return to the event loop before the duration time has elapsed, then the animation will immediately go to the end state?

  11. #8
    Join Date
    Mar 2015
    Posts
    8
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to update a graphics item immediately??

    Ok, I have solved it. Thank you guys very much for help.
    Last edited by Rabbitl96l; 4th April 2015 at 10:10.

  12. #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: How to update a graphics item immediately??

    ...and we won't ever know what the problem was
    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. ScrollArea for Graphics Item
    By karankumar1609 in forum Qt Programming
    Replies: 1
    Last Post: 11th July 2013, 13:38
  2. Replies: 0
    Last Post: 28th November 2011, 19:03
  3. Graphics item highlight
    By mukunda in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2011, 18:21
  4. Graphics Item
    By Maluko_Da_Tola in forum Newbie
    Replies: 4
    Last Post: 20th July 2010, 14:37
  5. Serialization and graphics item
    By prashant in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2009, 11:03

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.