Model-View Server

This is the server-side application that accompanies the Model-View Client.

This example showcases how to make a simple server program that displays and makes changes to a QTreeView which is made available on a Remote Objects network.

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

We start by creating a QRemoteObjectRegistryHost with which other Remote Objects will connect, be registered and then be advertised by. The model we create can then be easily acquired from the client side by just connecting to the registry.

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

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

Now we have to create the model we need. The exact implementation is available in the source code, to which you can navigate by pressing the link further down on this page. We also define which roles we want to expose to the Replica on the client side.

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

Here, we create the QRemoteObjectHost that connects to, and shares all its Remote Objects with, the Registry we created earlier. We then start remoting the model we just created with the name RemoteModel. We also pass the roles argument here.

    QTreeView view;
    view.setWindowTitle(QStringLiteral("SourceView"));
    view.setModel(sourceModel.get());
    view.show();

We then display the model with a QTreeView widget.

    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);

For the sake of keeping the example light-weight it performs some automated actions to affect the model shortly after the server application launches. These changes can then be seen on both the server and client side. You can also change the text in the fields on the server side and see it update on the client side.

Example project @ code.qt.io

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