IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Model-View Server

Developing a simple server program that displays and makes changes to a QTreeView which is made available on a Remote Objects network.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Model-View Server

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

 
Sélectionnez
    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.

 
Sélectionnez
    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.

 
Sélectionnez
    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.

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

We then display the model with a QTreeView widget.

 
Sélectionnez
    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

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+