PDA

View Full Version : Blinking QGraphicsTextItem text in QGraphicsView widget?



ashukla
5th January 2008, 10:02
Dear All!
How a way we can blink the QGraphicsTextItem in QGraphicsViewWidget?

Gopala Krishna
5th January 2008, 10:14
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. :)

wysota
5th January 2008, 10:54
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).

Gopala Krishna
5th January 2008, 11:11
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.


#include <QtGui>

const int BlinkDelay = 128;

class MyTextItem : public QGraphicsTextItem
{
public:
MyTextItem(QGraphicsItem *parent) : QGraphicsTextItem(parent)
{
startTimer(BlinkDelay);
draw = true;
}

void timerEvent(QTimerEvent *event)
{
if(!scene()) return;
draw = !draw;
update();
}

void paint(QPainter *p, const QStyleOptionGraphicsItem *o, QWidget *w)
{
if( draw) {
QGraphicsTextItem::paint(p, o, w);
}
}
bool draw;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene(0,0, 400,320);
QGraphicsView view(&scene);

MyTextItem item(0);
item.setPlainText("Hello there");
scene.addItem(&item);
item.setPos(125, 100);

view.show();
return app.exec();
}

wysota
5th January 2008, 12:43
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.

Gopala Krishna
5th January 2008, 16:01
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 :)

wysota
5th January 2008, 18:32
I mean kill the timer when item is hidden and start it again when it is shown. The itemChanged() method provides needed data.

momesana
22nd February 2008, 14:29
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! :confused:

Gopala Krishna
22nd February 2008, 15:27
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! :confused:

QGraphicsTextItem inherits QObject.
Look QGraphicsTextItem :)

momesana
22nd February 2008, 16:22
QGraphicsTextItem inherits QObject.
Look QGraphicsTextItem (http://doc.trolltech.com/latest/qgraphicstextitem.html) :)
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.