En esta página

Aplicación del servidor horario

Un servidor que comparte un objeto de tiempo con los clientes conectados.

La Aplicación Servidor de Tiempo instancia un objeto MinuteTimer y lo comparte con todas las Aplicaciones Cliente de Tiempo conectadas.

Terminal ejecutando remoteobjects_server y mostrando la hora actual

Definición de objetos remotos mediante un archivo REP

El archivo REP "timemodel.rep" en el directorio padre del ejemplo se utiliza para generar los archivos de cabecera utilizados por ambas aplicaciones. Para esta aplicación, el archivo generado "rep_timemodel_source.h" define MinuteTimerSource, la clase a subclasificar para la implementación de MinuteTimer, y otras clases relacionadas.

La clase TimeModel

La clase TimeModel en timemodel.h y timemodel.cpp implementa el objeto de tiempo a compartir. Utiliza un QBasicTimer para asegurar que el tiempo se actualiza llamando a la función miembro timerEvent.

void MinuteTimer::timerEvent(QTimerEvent *) { QTime now = QTime::currentTime(); if (now.second() == 59 && now.minute() == time.minute() && now.hour() == time.hour()) { // se acaba de pasar el tiempo, fuerzalo, espera 0.5 segundos extratime = 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(); }

Compartir una instancia de TimeModel

Se crean instancias de QRemoteObjectHost y un QRemoteObjectRegistryHost para alojar un objeto y tener un registro para encontrarlo. A continuación se crea un objeto MinuteTimer y se comparte utilizando la función miembro enableRemoting del objeto 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();
}

Proyecto de ejemplo @ code.qt.io

Ver también la sección Aplicación cliente de tiempo.

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