Results 1 to 10 of 10

Thread: how to show time in a GraphicsItem

  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default how to show time in a GraphicsItem

    hi friends,
    how to show time in a graphicsItem
    ex: inside a graphicsRectItem i want to show the current date and the time ... like digital clock example ....
    how can i use timer event in a painted text object ...?
    please help me ...

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: how to show time in a GraphicsItem

    Beside performance considerations you could create your own graphic item like
    Qt Code:
    1. MyGraphicsItem : public QGraphicsItem, QObject {
    2. Q_OBJECT
    To copy to clipboard, switch view to plain text mode 
    and then you have signals und slots and you could simply use a timer to trigger the paint method to update the clock. If you want better performance only update the region rect with the clock.


    EDIT: Or use "stacked" items: "subitem" is showing the time and is a child of your original item and lay out above it.

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: how to show time in a GraphicsItem

    thanks for the reply ...

    is the
    QGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    QWidget *widget);
    is like QPaintEvent() ... ? can i use update there ...?

    can u help me how to update a particular region rect()
    Last edited by wagmare; 22nd April 2009 at 14:54.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: how to show time in a GraphicsItem

    As you write you want to use a QGraphicsRectItem, so subclass that and do:
    Qt Code:
    1. MyGraphicsRectItem : public QGraphicsRectItem //...
    2. //...
    3. MyGraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    4. {
    5. // here you can use painter freely: painter->draw...
    6. QGraphicsRectItem::paint(painter, o, widget);
    7. }
    To copy to clipboard, switch view to plain text mode 

    For only update a certain rect use QGraphicsItem::update(const QRectF & rect = QRectF()) and implement the region in your paint method.

  5. The following user says thank you to Lykurg for this useful post:

    wagmare (22nd April 2009)

  6. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: how to show time in a GraphicsItem

    thanks ,
    i complete updating the area as
    starting a timer of 1000 msec
    my code
    Qt Code:
    1. :paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    2. {
    3. QPen pen(color, 2, Qt::SolidLine, Qt::RoundCap);
    4.  
    5. painter->setPen(pen);
    6. painter->drawRoundRect(rect());
    7. if(cond){
    8. draw(painter);
    9. timer->start(1000);
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. :draw(QPainter *painter)
    2. {
    3. QPointF value(28, 35);
    4. QPointF value2(15, 55);
    5. QTime time = QTime::currentTime();
    6. QString text = time.toString("hh:mm");
    7.  
    8. QDate date = QDate::currentDate();
    9. QString text2 = date.toString("dd.MM.yyyy");
    10.  
    11. painter->drawText(value,text);
    12. painter->drawText(value2, text2);
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    this is timer slot
    Qt Code:
    1. :slotUpdate()
    2. {
    3. update(0,0,29,37);
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    and it is working ... but is it a right way ...

  7. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    Default Re: how to show time in a GraphicsItem

    You could very well use QGraphicsTextItem and on timeout of the timer, simply set the text of the text item with QTime::currentTime().toString()

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

    wagmare (24th April 2009)

  9. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: how to show time in a GraphicsItem

    Hi,

    all what I am going to say, I never have done myself, but this is how I would optimize:

    • Store a QRectF (m_dateTimeRect) in which you display date and time
    • Check in paint if QStyleOptionGraphicsItem::exposedRect() equal m_dateTimeRect -> only draw the time. not the border...
    • Use QPointF value(28, 35); and QPointF value2(15, 55); directly...


    Lykurg

  10. The following user says thank you to Lykurg for this useful post:

    wagmare (24th April 2009)

  11. #8
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: how to show time in a GraphicsItem

    but is it possible to add an QGraphicsItem in painter inside the paint() function ...

  12. #9
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    116
    Thanked 42 Times in 41 Posts

    Default Re: how to show time in a GraphicsItem

    Quote Originally Posted by Lykurg View Post
    Hi,

    all what I am going to say, I never have done myself, but this is how I would optimize:

    • Store a QRectF (m_dateTimeRect) in which you display date and time
    • Check in paint if QStyleOptionGraphicsItem::exposedRect() equal m_dateTimeRect -> only draw the time. not the border...
    • Use QPointF value(28, 35); and QPointF value2(15, 55); directly...


    Lykurg
    thanks very much for the reply .....
    i will try this and report

  13. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: how to show time in a GraphicsItem

    Quote Originally Posted by wagmare View Post
    but is it possible to add an QGraphicsItem in painter inside the paint() function ...
    You have following opportunities:
    • Do all the stuff by yourself inside the paint
    • Let Qt do some things for you.


    If the last, decide from which QGraphicsXXXItem you want subclass. As I have understand you, you want a border and inside date and time. So I would subclass MyGraphicsRectItem as previously written. Then you don't have to draw your border yourself. Use
    Qt Code:
    1. QGraphicsRectItem::paint(painter, o, widget);
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. painter->drawRoundRect(rect());
    To copy to clipboard, switch view to plain text mode 

    Or use a text item and set date and time via setText and draw the border yourself...

    Lykurg

Similar Threads

  1. QAbstractItemView - Show all contents all the time...
    By youkai in forum Qt Programming
    Replies: 5
    Last Post: 31st August 2008, 17:54
  2. How to constantly refresh time on a view
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 23rd June 2008, 13:44
  3. Show System Time Stamp ??
    By dheeraj in forum Qwt
    Replies: 3
    Last Post: 21st April 2008, 12:54
  4. Show progress of a time consuming operation
    By rainman110 in forum Newbie
    Replies: 7
    Last Post: 10th February 2008, 13:07
  5. QDateTime GMT add sec. or - sec. from locale time....
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2007, 17:39

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.