Archived:Blink the QGraphicsTextItem in QGraphicsViewWidget
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
This code snippet shows how to blink text (a QGraphicsTextItem) in a QGraphicsViewWidget. The GraphicsView is refreshed with the help of update() function. The text is then repainted with the help of paint() method.
Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Article
Keywords: QGraphicsTextItem,QGraphicsItem
Created: mind_freak
(06 Aug 2009)
Last edited: hamishwillee
(11 Oct 2012)
Code Snippet
Main cpp
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
const int delay = 200;//set delay as per required
class MyTextItem : public QGraphicsTextItem
{
public:
MyTextItem()
{
startTimer(delay); // start a timer
drawItem = true;
}
void timerEvent(QTimerEvent *)
{
// no update if the item is not visible
if(!isVisible())
return;
drawItem = !drawItem;
update();
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
if (drawItem) {
// call the parent's implementation to draw the text
QGraphicsTextItem::paint(painter, option, widget);
}
}
private:
bool drawItem;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QGraphicsScene scene(0,0, 400,320);
QGraphicsView view(&scene);
MyTextItem item;
item.setPlainText("Qt rocks on Symbian!");
scene.addItem(&item);
item.setPos(125, 100);
view.show();
return app.exec();
}


The article is quite good, thanks to the example provided. The example is really interesting, it shows good object-oriented programming with the overloading of paint() and timerEvent(). The code follows the convention of Qt, which is great.
For me, there are two failures in this code as an example. First, if the item is not visible, the timer will still fire every 200ms, which will just waste the battery. Secondly, there will be as many timers as instances of MyTextItem, which is a big waste of resource. I would not expect this to be covered in a example so simple, but that should have been cited in the text.
Speaking of the text, there is definitely a lack of explanation. The requirements are described in the beginning, but there is not a single line of explanation about what is done in the example.
I would not have given the "Reviewer Approved". The article is ok, but it could be much better.
12 Sep
2009