시간 서버 애플리케이션

연결된 클라이언트와 시간 개체를 공유하는 서버입니다.

시간 서버 애플리케이션은 MinuteTimer 개체를 인스턴스화하여 연결된 모든 시간 클라이언트 애플리케이션과 공유합니다.

REP 파일을 사용하여 원격 객체 정의하기

예제의 상위 디렉터리에 있는 REP 파일 "timemodel.rep"는 두 애플리케이션에서 사용하는 헤더 파일을 생성하는 데 사용됩니다. 이 애플리케이션의 경우 생성된 "rep_timemodel_source.h" 파일은 MinuteTimerSource, MinuteTimer 의 구현을 위한 서브클래스 및 기타 관련 클래스를 정의합니다.

타임모델 클래스

timemodel.h 및 timemodel.cpp의 TimeModel 클래스는 공유할 시간 객체를 구현합니다. 이 클래스는 timerEvent 멤버 함수를 호출하여 시간을 업데이트하기 위해 QBasicTimer 을 사용합니다.

void MinuteTimer::timerEvent(QTimerEvent *) { QTime now = QTime::currentTime(); if (now.second()== 59 && now.minute()== time.minute() && now.hour()== time.hour()) { // 방금 놓친 시간 틱 오버, 강제 0.5초 더 기다립니다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(); }

TimeModel 인스턴스 공유하기

QRemoteObjectHost 인스턴스와 QRemoteObjectRegistryHost 인스턴스가 생성되어 객체를 호스팅하고 이를 찾을 수 있는 레지스트리를 갖습니다. 그런 다음 MinuteTimer 객체가 생성되고 QRemoteObjectRegistryHost 객체의 enableRemoting 멤버 함수를 사용하여 공유됩니다.

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

예제 프로젝트 @ 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.