How to change the current time with Text
-
If you haven't seen it already, there is a "Clocks" example in examples/declarative/toys/clocks that might also be useful to look at.
Regards,
Michael -
Oh, that's true! I completely forgot that! Thanks!
Just for the record: a Timer is used in that example.
-
[quote author="disperso" date="1292206056"]Oh, that's true! I completely forgot that! Thanks!
Just for the record: a Timer is used in that example.[/quote]
It seems I need to give some time to explore all the examples.
-
Another approach, just to highlight that there are different ways to do stuff, is to use C++ to make a simple clock element. This element would have properties for time and date, and update them (complete with signal) when needed. The timer would therefore move to this C++ part.
In QML, you can then just do something like this:
@Item {
Clock {
id: clock
timeFormat: 'hh:mm:ss'
}Text { id: timeDisplay text: clock.time } Text { id: dateDisplay text: clock.date }
}
@The code needed for the Clock element would not be hard at all, just a few lines of code.
Edit:
I just tried to implement a Clock element. It wasn't hard:
clock.h
@
#ifndef CLOCK_H
#define CLOCK_H#include <QDeclarativeItem>
class QTimer;
class Clock : public QDeclarativeItem
{
Q_OBJECT
Q_PROPERTY (QString time READ time NOTIFY timeChanged)
Q_PROPERTY (QString date READ date NOTIFY dateChanged)
Q_PROPERTY (QString timeFormat READ timeFormat WRITE setTimeFormat NOTIFY timeFormatChanged)
Q_PROPERTY (QString datFormate READ dateFormat WRITE setDateFormat NOTIFY dateFormatChanged)public:
explicit Clock(QDeclarativeItem *parent = 0);QString time() const {return m_time;} QString date() const {return m_date;} QString timeFormat() const {return m_timeFormat;} QString dateFormat() const {return m_dateFormat;} void setTimeFormat(const QString& format); void setDateFormat(const QString& format);
signals:
void timeChanged();
void dateChanged();
void timeFormatChanged();
void dateFormatChanged();public slots:
private slots:
void updateTimeDate();private:
QString m_time;
QString m_date;
QString m_timeFormat;
QString m_dateFormat;QTimer* m_timer;
};
#endif // CLOCK_H
@clock.cpp:
@
#include "clock.h"
#include <QTimer>
#include <QDateTime>
#include <QLocale>Clock::Clock(QDeclarativeItem *parent) :
QDeclarativeItem(parent),
m_timer(new QTimer(this))
{
QLocale loc;
m_timeFormat = loc.timeFormat();
m_dateFormat = loc.dateFormat();m_timer->setInterval(200); connect(m_timer, SIGNAL(timeout()), this, SLOT(updateTimeDate())); m_timer->start(); updateTimeDate();
}
void Clock::setTimeFormat(const QString& format)
{
if (format != m_timeFormat) {
m_timeFormat = format;
emit timeFormatChanged();
updateTimeDate();
}
}void Clock::setDateFormat(const QString& format)
{
if (format != m_dateFormat) {
m_dateFormat = format;
emit dateFormatChanged();
updateTimeDate();
}
}void Clock::updateTimeDate()
{
QDateTime dt = QDateTime::currentDateTime();QString time = dt.time().toString(m_timeFormat); if (time != m_time) { m_time = time; emit timeChanged(); } QString date = dt.date().toString(m_dateFormat); if (date != m_date) { m_date = date; emit dateChanged(); }
}
@Register the class for use with QML in a line like this:
@ qmlRegisterType<Clock>("com.test.qmlcomponent", 1, 0, "Clock");@In the QML, you can then import the Clock element like so:
@import com.test.qmlcomponent 1.0@
and use it as illustrated in my example at the top.If you want, you can extend the Clock class to expose the different time units seperately too. That makes it easy to use it for a clock that you display with other means than just a text, like in analog form.
-
This thread deserves becoming a wiki...
It is very interesting to have developers trying to come up with different approaches to the same problem. Maybe we could make that a "regular" activity. Someone suggests a problem and we spend few days trying to propose different solutions.
Anybody up for it?