모델-뷰 서버

Remote Objects 네트워크에서 사용할 수 있는 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);

예제의 규모를 가볍게 유지하기 위해, 서버 애플리케이션이 시작된 직후 모델에 영향을 미치는 몇 가지 자동화된 작업이 수행됩니다. 이러한 변경 사항은 서버와 클라이언트 양쪽에서 확인할 수 있습니다. 또한 서버 측에서 필드의 텍스트를 변경하면 클라이언트 측에서 업데이트된 내용을 확인할 수 있습니다.

예제 프로젝트 @ code.qt.io

© 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.