Results 1 to 10 of 10

Thread: Blinking QGraphicsTextItem text in QGraphicsView widget?

  1. #1
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Lightbulb Blinking QGraphicsTextItem text in QGraphicsView widget?

    Dear All!
    How a way we can blink the QGraphicsTextItem in QGraphicsViewWidget?
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Blinking QGraphicsTextItem text in QGraphicsView widget?

    A crazy idea is to toggle between hide and show in QGraphicsTextItem::timerEvent()
    (it is a QObject).
    But i really don't know how this works out to be.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    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: Blinking QGraphicsTextItem text in QGraphicsView widget?

    I wouldn't do that. Showing and hiding the item constantly would probably cause some internal data to be updated all the time. It is much simpler to subclass and simply not draw the item when it is to blink. This of course implies using a timer somewhere (not necessarily in the item itself).

  4. #4
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Blinking QGraphicsTextItem text in QGraphicsView widget?

    Quote Originally Posted by wysota View Post
    I wouldn't do that. Showing and hiding the item constantly would probably cause some internal data to be updated all the time. It is much simpler to subclass and simply not draw the item when it is to blink. This of course implies using a timer somewhere (not necessarily in the item itself).
    Yeah, indeed this is the right approach to go
    Just came up with following code.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. const int BlinkDelay = 128;
    4.  
    5. class MyTextItem : public QGraphicsTextItem
    6. {
    7. public:
    8. MyTextItem(QGraphicsItem *parent) : QGraphicsTextItem(parent)
    9. {
    10. startTimer(BlinkDelay);
    11. draw = true;
    12. }
    13.  
    14. void timerEvent(QTimerEvent *event)
    15. {
    16. if(!scene()) return;
    17. draw = !draw;
    18. update();
    19. }
    20.  
    21. void paint(QPainter *p, const QStyleOptionGraphicsItem *o, QWidget *w)
    22. {
    23. if( draw) {
    24. QGraphicsTextItem::paint(p, o, w);
    25. }
    26. }
    27. bool draw;
    28. };
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. QApplication app(argc, argv);
    33. QGraphicsScene scene(0,0, 400,320);
    34. QGraphicsView view(&scene);
    35.  
    36. MyTextItem item(0);
    37. item.setPlainText("Hello there");
    38. scene.addItem(&item);
    39. item.setPos(125, 100);
    40.  
    41. view.show();
    42. return app.exec();
    43. }
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  5. #5
    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: Blinking QGraphicsTextItem text in QGraphicsView widget?

    It'd be smarter to start and stop the timer from within QGraphicsItem::itemChange() when ItemVisibleChange occurs. There is no point in triggering the timer if the item is not visible.

  6. #6
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Blinking QGraphicsTextItem text in QGraphicsView widget?

    Quote Originally Posted by wysota View Post
    It'd be smarter to start and stop the timer from within QGraphicsItem::itemChange() when ItemVisibleChange occurs. There is no point in triggering the timer if the item is not visible.
    You mean to just check whether the item is visible before triggering timer. I mean kill timers when hidden and enable it on show. I guess this is what you meant.
    Not a problem to implement
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  7. #7
    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: Blinking QGraphicsTextItem text in QGraphicsView widget?

    I mean kill the timer when item is hidden and start it again when it is shown. The itemChanged() method provides needed data.

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Blinking QGraphicsTextItem text in QGraphicsView widget?

    How comes startTimer and timerEvent are available in MyTextItem even though it and its ancestors do not inherit from QObject? and it is only QObject that provides these methods.
    I am pretty confused about the fact that the code compiles at all!

  9. #9
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Blinking QGraphicsTextItem text in QGraphicsView widget?

    Quote Originally Posted by momesana View Post
    How comes startTimer and timerEvent are available in MyTextItem even though it and its ancestors do not inherit from QObject? and it is only QObject that provides these methods.
    I am pretty confused about the fact that the code compiles at all!
    QGraphicsTextItem inherits QObject.
    Look QGraphicsTextItem
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Blinking QGraphicsTextItem text in QGraphicsView widget?

    Quote Originally Posted by Gopala Krishna View Post
    QGraphicsTextItem inherits QObject.
    Look QGraphicsTextItem
    You are right. I must have been blind when going through its documentation or maybe I had unintentolly clicked on the QGraphicsItem documentation in assistant.

    Thank you.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Creating a widget on a QGraphicsView
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 9th August 2007, 17:54
  3. Editable text in QGraphicsView
    By wysota in forum Qt Programming
    Replies: 8
    Last Post: 24th February 2007, 15:30

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.