Results 1 to 5 of 5

Thread: QGraphicsObject text not updating on QGraphicsView widget

  1. #1
    Join Date
    Nov 2014
    Posts
    23
    Thanks
    4

    Default QGraphicsObject text not updating on QGraphicsView widget

    I have created an object using QGraphicsObject as Base class, in this object's paint function i am using painter->drawText() function to draw a text at a particular coordinate..... The text gets updated only when i do some mouse action inside QGraphicsView Widget or outside the application......if i put a timer and constantly trying to update text , its not getting updates......

    I need to be able to update Text with timer also.....any help is appreciated

    Here is my code for paint function

    void Text_Display:aint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    painter->setFont(font);
    painter->setPen(pen);
    painter->drawText(pos.x(),pos.y(),text);
    }


    -> Paint function is being called repeatatly and text variable is getting updated too , only thing is it's not getting updated on screen
    Last edited by vinothrajendran; 2nd July 2015 at 10:10.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,720
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QGraphicsObject text not updating on QGraphicsView widget

    So is the instance of Text_Display for which the paint() method is being called the same one you think you have on-screen in your scene? If you have multiple instances of this in your scene, are you sure that the one being updated is actually visible in the view? Is "pos" actually within the bounds of the Text_Display bounding rect?

    Use [CODE] tags when posting source code, please.

  3. #3
    Join Date
    Nov 2014
    Posts
    23
    Thanks
    4

    Default Re: QGraphicsObject text not updating on QGraphicsView widget

    Now i kept only one instance of Text_Display class and commented other...still i have the same problem.......and i think i have pos variable value within bounding rectangle....

    code is below:

    Qt Code:
    1. QRectF Text_Display::boundingRect() const
    2. {
    3.  
    4. return QRectF(pos.x(), pos.y(), 200, 200);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Text_Display::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->setFont(font);
    4. painter->setPen(pen);
    5. painter->drawText(pos.x(),pos.y(),text);
    6. }
    To copy to clipboard, switch view to plain text mode 


    and thanks for replying........

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,720
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QGraphicsObject text not updating on QGraphicsView widget

    pos() is in the coordinate system of the parent of your QGraphicsObject, not the Text_Display instance itself. So drawing using those coordinates or returning them as a corner of the boundingRect() is incorrect. You are most likely drawing outside the bounds of your object and returning an invalid rect.

    Remove the references to pos(), change x and y both to zero and see what happens. If you want the text to be centered in the box, then you'll need to change the alignment and/or change the drawText() coordinates to move the text where you want it.

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

    vinothrajendran (6th July 2015)

  6. #5
    Join Date
    Nov 2014
    Posts
    23
    Thanks
    4

    Thumbs up Re: QGraphicsObject text not updating on QGraphicsView widget

    Problem solved......Inside my class i added QGraphicsScene pointer (scene) and made it to point to parent class member function scene()..Then i added one line inside paint()

    the code :

    Qt Code:
    1. void Text_Display::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    2. {
    3. painter->setFont(font);
    4. painter->setPen(pen);
    5. painter->drawText(pos.x(),pos.y(),text);
    6. scene->update(); // Just this line
    7. }
    To copy to clipboard, switch view to plain text mode 

    Now my text is getting updated as i wish...

    Thanks for ur help...........

Similar Threads

  1. Replies: 1
    Last Post: 28th July 2011, 12:28
  2. Listwidgetitem text is chpopped while updating widget
    By sudhansu in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2010, 16:37
  3. QGraphicsView not updating properly.
    By spraff in forum Qt Programming
    Replies: 0
    Last Post: 3rd July 2009, 11:36
  4. Replies: 9
    Last Post: 22nd February 2008, 17:22
  5. Updating QGraphicsView
    By has2k1 in forum Newbie
    Replies: 2
    Last Post: 8th April 2007, 22:02

Tags for this Thread

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.