タイムサーバーアプリケーション
接続されたクライアントと時刻オブジェクトを共有するサーバー。
タイムサーバーアプリケーションは、MinuteTimer オブジェクトをインスタンス化し、接続されているすべてのタイムクライアントアプリケーションとそれを共有します。

REPファイルを使用したリモートオブジェクトの定義
サンプルの上位ディレクトリにあるREPファイル「timemodel.rep」は、両方のアプリケーションで使用されるヘッダーファイルを生成するために使用されます。このアプリケーションの場合、生成された「rep_timemodel_source.h」ファイルでは、MinuteTimerSource 、MinuteTimer の実装のためにサブクラス化するクラス、およびその他の関連クラスが定義されています。
TimeModel クラス
timemodel.h および timemodel.cpp にあるTimeModel クラスは、共有される時間オブジェクトを実装しています。このクラスは、QBasicTimer を使用して、timerEvent メンバー関数を呼び出すことで時間が確実に更新されるようにしています。
voidMinuteTimer::timerEvent(QTimerEvent*)
{
QTime now=QTime::currentTime();
if(now.second()== 59&&now.minute()==time.minute()&&now.hour()==time.hour()) {
// タイムティックをわずかに逃したため、強制的に実行し、さらに0.5秒待機する
time=time.addSecs(60);
timer.start(60500, this);
}else{
time=now;
timer.start(60000- time.second()* 1000, this);
}
qDebug()<<"Time"<<time;
setHour(time.hour());
setMinute(time.minute());
emittimeChanged();
}TimeModelのインスタンスの共有
QRemoteObjectHost およびQRemoteObjectRegistryHost のインスタンスは、オブジェクトを格納し、それを検索するためのレジストリを持つために作成されます。その後、MinuteTimer オブジェクトが作成され、QRemoteObjectRegistryHost オブジェクトのenableRemoting メンバ関数を用いて共有されます。
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, &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(&timer);
Q_UNUSED(timer)
return app.exec();
}「Time Client Application」も参照してください 。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.