モデル・ビュー・サーバー
リモートオブジェクトネットワーク上で利用可能な QTreeView を表示・更新する、シンプルなサーバープログラムの開発。
これは、モデル・ビュー・クライアントに付随するサーバーサイドアプリケーションです。
QRemoteObjectRegistryHost node(QUrl(QStringLiteral("local:registry")));まず、他のリモートオブジェクトが接続し、登録され、その後アドバタイズされるためのQRemoteObjectRegistryHost を作成します。作成したモデルは、レジストリに接続するだけでクライアント側から簡単に取得できます。
std::unique_ptr<QStandardItemModel> sourceModel = createModel();
QList<int> roles;
roles << Qt::DisplayRole << Qt::BackgroundRole;次に、必要なモデルを作成します。具体的な実装内容はソースコードに記載されており、このページの下部にあるリンクをクリックして参照できます。また、クライアント側のReplicaに対して公開したいロールも定義します。
QRemoteObjectHost node2(QUrl(QStringLiteral("local:replica")), QUrl(QStringLiteral("local:registry")));
node2.enableRemoting(sourceModel.get(), QStringLiteral("RemoteModel"), roles);ここでは、先ほど作成したレジストリに接続し、すべてのリモートオブジェクトを共有するQRemoteObjectHost を作成します。次に、RemoteModel という名前で、先ほど作成したモデルのリモート処理を開始します。ここでは、roles引数も渡します。
QTreeView view;
view.setWindowTitle(QStringLiteral("SourceView"));
view.setModel(sourceModel.get());
view.show();続いて、QTreeViewウィジェットを使用してモデルを表示します。
TimerHandler handler;
handler.model = sourceModel.get();
QTimer::singleShot(5000, &handler, &TimerHandler::changeData);
QTimer::singleShot(10000, &handler, &TimerHandler::insertData);
QTimer::singleShot(11000, &handler, &TimerHandler::changeFlags);
QTimer::singleShot(12000, &handler, &TimerHandler::removeData);
QTimer::singleShot(13000, &handler, &TimerHandler::moveData);この例を簡潔に保つため、サーバーアプリケーションの起動直後に、モデルに影響を与えるいくつかの自動処理が実行されます。これらの変更は、サーバー側とクライアント側の両方で確認できます。また、サーバー側のフィールドのテキストを変更すると、クライアント側で更新される様子を確認することもできます。
© 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.