PDA

View Full Version : QGraphicsObject text not updating on QGraphicsView widget



vinothrajendran
2nd July 2015, 09:49
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::paint(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

d_stranz
2nd July 2015, 19:44
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.

vinothrajendran
3rd July 2015, 11:19
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:



QRectF Text_Display::boundingRect() const
{

return QRectF(pos.x(), pos.y(), 200, 200);
}




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



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

d_stranz
6th July 2015, 03:45
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.

vinothrajendran
6th July 2015, 07:33
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 :



void Text_Display::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->setFont(font);
painter->setPen(pen);
painter->drawText(pos.x(),pos.y(),text);
scene->update(); // Just this line
}


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

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