Time Server Application▲
The Time Server Application instantiates a MinuteTimer object and shares it with all connected Time Client Applications.
I. Defining Remote Objects using a REP-file▲
The REP-file "timemodel.rep" in the parent directory of the example is used to generate the header-files used by both applications. For this application, the generated "rep_timemodel_source.h" file defines MinuteTimerSource, the the class to subclass for the implementation of MinuteTimer, and other related classes.
II. The TimeModel Class▲
The TimeModel class in timemodel.h and timemodel.cpp implements the time object to share. It uses a QBasicTimer to ensure that the time is updated by calling the timerEvent member function.
void
MinuteTimer::
timerEvent(QTimerEvent *
)
{
QTime now =
QTime::
currentTime();
if
(now.second() ==
59
&
amp;&
amp; now.minute() ==
time.minute() &
amp;&
amp; now.hour() ==
time.hour()) {
// just missed time tick over, force it, wait extra 0.5 seconds
time =
time.addSecs(60
);
timer.start(60500
, this
);
}
else
{
time =
now;
timer.start(60000
-
time.second()*
1000
, this
);
}
qDebug()&
lt;&
lt;"Time"
&
lt;&
lt;time;
setHour(time.hour());
setMinute(time.minute());
emit timeChanged();
}
III. Sharing an Instance of TimeModel▲
int
main(int
argc, char
*
argv[])
{
QCoreApplication app(argc, argv);
#if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) || defined(Q_OS_QNX)
signal(SIGINT, &
amp;unix_handler);
#elif defined(Q_OS_WIN32)
SetConsoleCtrlHandler((PHANDLER_ROUTINE)WinHandler, TRUE);
#endif
QRemoteObjectHost node(QUrl(QStringLiteral("local:replica"
)),QUrl(QStringLiteral("local:registry"
)));
QRemoteObjectRegistryHost node2(QUrl(QStringLiteral("local:registry"
)));
MinuteTimer timer;
node2.enableRemoting(&
amp;timer);
Q_UNUSED(timer)
return
app.exec();
}
IV. Example project▲
V. See Also▲
See also Time Client Application