タイムサーバーアプリケーション
接続されたクライアントと時間オブジェクトを共有するサーバー。
タイムサーバーアプリケーションは、MinuteTimer
オブジェクトをインスタンス化し、接続されているすべてのタイムクライアントアプリケーションと共有します。
REPファイルを使用したリモートオブジェクトの定義
サンプルの親ディレクトリにあるREPファイル "timemodel.rep "は、両方のアプリケーションで使用されるヘッダーファイルを生成するために使用されます。このアプリケーションでは、生成された "rep_timemodel_source.h "ファイルが、MinuteTimerSource
、MinuteTimer
の実装のためにサブクラス化するクラス、およびその他の関連クラスを定義しています。
TimeModel クラス
timemodel.hとtimemodel.cppのTimeModel
クラスは、共有する時間オブジェクトを実装します。timerEvent
メンバ関数を呼び出すことで、時刻が更新されるようにQBasicTimer
。
voidMinuteTimer::timerEvent(QTimerEvent*){ QTimenow=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(); }
タイムクライアントアプリケーションも参照してください 。
© 2025 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.