PDA

View Full Version : how to show time in a GraphicsItem



wagmare
22nd April 2009, 11:12
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 ...

Lykurg
22nd April 2009, 12:39
Beside performance considerations you could create your own graphic item like

MyGraphicsItem : public QGraphicsItem, QObject {
Q_OBJECT
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.

wagmare
22nd April 2009, 13:49
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()

Lykurg
22nd April 2009, 14:03
As you write you want to use a QGraphicsRectItem, so subclass that and do:

MyGraphicsRectItem : public QGraphicsRectItem //...
//...
MyGraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
// here you can use painter freely: painter->draw...
QGraphicsRectItem::paint(painter, o, widget);
}


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

wagmare
24th April 2009, 07:27
thanks ,
i complete updating the area as
starting a timer of 1000 msec
my code


:paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
QPen pen(color, 2, Qt::SolidLine, Qt::RoundCap);

painter->setPen(pen);
painter->drawRoundRect(rect());
if(cond){
draw(painter);
timer->start(1000);
}
}




:draw(QPainter *painter)
{
QPointF value(28, 35);
QPointF value2(15, 55);
QTime time = QTime::currentTime();
QString text = time.toString("hh:mm");

QDate date = QDate::currentDate();
QString text2 = date.toString("dd.MM.yyyy");

painter->drawText(value,text);
painter->drawText(value2, text2);

}


this is timer slot


:slotUpdate()
{
update(0,0,29,37);

}


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

aamer4yu
24th April 2009, 08:06
You could very well use QGraphicsTextItem and on timeout of the timer, simply set the text of the text item with QTime::currentTime().toString() :)

Lykurg
24th April 2009, 08:43
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

wagmare
24th April 2009, 09:43
but is it possible to add an QGraphicsItem in painter inside the paint() function ...

wagmare
24th April 2009, 09:44
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

Lykurg
24th April 2009, 09:52
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

QGraphicsRectItem::paint(painter, o, widget);instead of
painter->drawRoundRect(rect());

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

Lykurg