모델 뷰 서버

원격 객체 네트워크에서 사용할 수 있는 QTreeView를 표시하고 변경하는 간단한 서버 프로그램을 개발합니다.

이것은 Model-View 클라이언트와 함께 제공되는 서버 측 응용 프로그램입니다.

    QRemoteObjectRegistryHost node(QUrl(QStringLiteral("local:registry")));

먼저 다른 원격 객체를 연결하고 등록한 다음 광고할 QRemoteObjectRegistryHost 을 생성합니다. 그러면 클라이언트 측에서 레지스트리에 연결하기만 하면 우리가 만든 모델을 쉽게 얻을 수 있습니다.

    std::unique_ptr<QStandardItemModel> sourceModel = createModel();

    QList<int> roles;
    roles << Qt::DisplayRole << Qt::BackgroundRole;

이제 필요한 모델을 만들어야 합니다. 정확한 구현은 소스 코드에서 확인할 수 있으며, 이 페이지의 아래 링크를 누르면 해당 페이지로 이동할 수 있습니다. 또한 클라이언트 측에서 레플리카에 노출할 역할을 정의합니다.

    QRemoteObjectHost node2(QUrl(QStringLiteral("local:replica")), QUrl(QStringLiteral("local:registry")));
    node2.enableRemoting(sourceModel.get(), QStringLiteral("RemoteModel"), roles);

여기서는 앞서 만든 레지스트리에 연결하고 모든 원격 개체를 공유하는 QRemoteObjectHost 을 만듭니다. 그런 다음 RemoteModel 라는 이름으로 방금 만든 모델을 원격으로 시작합니다. 여기에 역할 인수도 전달합니다.

    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

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