In questa pagina

Applicazione Time Server

Un server che condivide un oggetto temporale con i client connessi.

L'applicazione Time Server istanzia un oggetto MinuteTimer e lo condivide con tutte le applicazioni Time Client connesse.

Terminale su cui è in esecuzione remoteobjects_server e che mostra l'ora corrente

Definizione di oggetti remoti tramite un file REP

Il file REP "timemodel.rep", presente nella directory principale dell'esempio, viene utilizzato per generare i file di intestazione utilizzati da entrambe le applicazioni. Per questa applicazione, il file generato "rep_timemodel_source.h" definisce MinuteTimerSource, la classe da cui derivare per l'implementazione di MinuteTimer e altre classi correlate.

La classe TimeModel

La classe ` TimeModel `, presente nei file `timemodel.h` e `timemodel.cpp`, implementa l’oggetto temporale da condividere. Utilizza un ` QBasicTimer ` per garantire che l’ora venga aggiornata chiamando la funzione membro ` timerEvent `.

void MinuteTimer::timerEvent(QTimerEvent*)
{
    QTime now = QTime::currentTime();
    if (now.second()== 59&& now.minute()== time.minute()&& now.hour()== time.hour()) {
        // il cambio di tempo è appena sfuggito, forzarlo, attendere 0,5 secondi in più
        time = time.addSecs(60);
        timer.start(60500, this);
    } else {
        time = now;
        timer.start(60000- time.second()* 1000, this);
    }
    qDebug()<<"Time"<<time;
    setHour(time.hour());
    setMinute(time.minute());
    emit timeChanged();
}

Condivisione di un'istanza di TimeModel

Le istanze di ` QRemoteObjectHost ` e ` QRemoteObjectRegistryHost ` vengono create per ospitare un oggetto e disporre di un registro per individuarlo. Viene quindi creato un oggetto ` MinuteTimer `, che viene condiviso utilizzando la funzione membro ` enableRemoting ` dell'oggetto ` QRemoteObjectRegistryHost `.

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    #if defined(Q_OS_UNIX) || defined(Q_OS_LINUX) || defined(Q_OS_QNX)
        signal(SIGINT, &unix_handler);
    #elif defined(Q_OS_WIN32)
        SetConsoleCtrlHandler((PHANDLER_ROUTINE)WinHandler, TRUE);
    #endif
    QRemoteObjectHost node(QUrl(QStringLiteral("local:replica")),QUrl(QStringLiteral("local:registry")));
    QRemoteObjectRegistryHost node2(QUrl(QStringLiteral("local:registry")));
    MinuteTimer timer;
    node2.enableRemoting(&timer);

    Q_UNUSED(timer)
    return app.exec();
}

Progetto di esempio su code.qt.io

Vedi anche Applicazione client Time.

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