날씨 정보
날씨 정보 예제는 사용자의 현재 위치를 사용하여 C++ 플러그인에서 웹 서비스에서 로컬 콘텐츠를 검색하는 방법을 보여줍니다. Qt Quick의 C++ 플러그인에서 Qt Positioning.
Key Qt Positioning 클래스를 사용합니다:
예제 실행하기
에서 예제를 실행하려면 Qt Creator에서 Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 예제 빌드 및 실행하기를 참조하세요.
날씨 데이터 제공업체
이 예에서는 서로 관련이 없는 여러 날씨 데이터 공급업체를 사용합니다:
사용할 공급자는 런타임에 자동으로 선택되며 선택한 공급자를 사용할 수 없는 경우 변경할 수 있습니다. 그러나 수동으로 지정할 수는 없습니다.
참고: 모든 제공업체에 무료 요금제가 사용되므로 날씨 요청 횟수에 일정한 제한이 있습니다. 한도를 초과하면 해당 제공업체는 일시적으로 사용할 수 없게 됩니다. 모든 제공업체를 사용할 수 없는 경우 애플리케이션에서 날씨 정보를 표시할 수 없습니다. 이 경우 하나 이상의 공급자가 다시 사용할 수 있게 될 때까지 기다려야 합니다.
애플리케이션 데이터 모델
이 예제의 핵심 부분은 WeatherData
및 AppModel
클래스에 포함된 애플리케이션의 데이터 모델입니다. WeatherData
는 HTTP 서비스에서 가져온 날씨 정보를 나타냅니다. 간단한 데이터 클래스이지만 나중에 QML에 멋지게 노출하기 위해 Q_PROPERTY 을 사용합니다. 또한 QML_ANONYMOUS 매크로를 사용하여 QML에서 인식되도록 합니다.
class WeatherData : public QObject { Q_OBJECT Q_PROPERTY(QString dayOfWeek READ dayOfWeek WRITE setDayOfWeek NOTIFY dataChanged) Q_PROPERTY(QString weatherIcon READ weatherIcon WRITE setWeatherIcon NOTIFY dataChanged) Q_PROPERTY(QString weatherDescription READ weatherDescription WRITE setWeatherDescription NOTIFY dataChanged) Q_PROPERTY(QString temperature READ temperature WRITE setTemperature NOTIFY dataChanged) QML_ANONYMOUS public: explicit WeatherData(QObject *parent = 0); WeatherData(const WeatherData &other); WeatherData(const WeatherInfo &other); QString dayOfWeek() const; QString weatherIcon() const; QString weatherDescription() const; QString temperature() const; void setDayOfWeek(const QString &value); void setWeatherIcon(const QString &value); void setWeatherDescription(const QString &value); void setTemperature(const QString &value); signals: void dataChanged(); };
AppModel
전체 애플리케이션의 상태를 모델링합니다. 시작할 때 QGeoPositionInfoSource::createDefaultSource()를 사용하여 플랫폼의 기본 위치 소스를 가져옵니다.
AppModel::AppModel(QObject *부모) : QObject(parent),d(new AppModelPrivate) { d->src = QGeoPositionInfoSource::createDefaultSource(this); if (d->src) { d->useGps = true; connect(d->src, &.QGeoPositionInfoSource::positionUpdated, this, &AppModel::positionUpdated); connect(d->src, &QGeoPositionInfoSource::errorOccurred, this, &AppModel::positionError);#if QT_CONFIG(permissions) QLocationPermission permission; permission.setAccuracy(QLocationPermission::Precise); permission.setAvailability(QLocationPermission::WhenInUse); switch (qApp->checkPermission(permission)) { case Qt::PermissionStatus::Undetermined: qApp->requestPermission(permission, [this](const QPermission& permission) { if (permission.status()==. Qt::PermissionStatus::Granted) d->src->startUpdates(); elsepositionError(QGeoPositionInfoSource::AccessError); }); break; case Qt::PermissionStatus::Denied: qWarning("Location permission is denied"); positionError(QGeoPositionInfoSource::AccessError); break; case Qt::PermissionStatus::Granted: d->src->startUpdates(); break; }#else d->src->startUpdates();#endif} else { d->useGps = false; d->city = "Brisbane"; emit cityChanged(); requestWeatherByCity(); } QTimer *refreshTimer = new QTimer(this); connect(refreshTimer, &.QTimer::timeout, this, &AppModel::refreshWeather); using namespace std::chrono; refreshTimer->start(60s); }
기본 소스를 사용할 수 없는 경우, 정적 위치를 가져와서 해당 위치에 대한 날씨를 가져옵니다. 그러나 위치 소스가 있는 경우 positionUpdated() 신호를 AppModel
의 슬롯에 연결하고 startUpdates()를 호출하여 기기 위치의 정기적인 업데이트를 시작합니다.
위치 업데이트가 수신되면 반환된 좌표의 경도와 위도를 사용하여 지정된 위치에 대한 날씨 데이터를 검색합니다.
void AppModel::positionUpdated(QGeoPositionInfo gpsPos) { d->coord = gpsPos.coordinate(); if (!d->useGps) return; requestWeatherByCoordinates(); }
이 프로세스를 UI에 알리기 위해 새로운 도시가 사용되면 cityChanged()
신호가, 날씨 업데이트가 발생할 때마다 weatherChanged()
신호가 전송됩니다.
이 모델은 또한 QML에서 사용할 수 있는 QML_ELEMENT 매크로를 사용합니다.
class AppModel : public QObject { Q_OBJECT Q_PROPERTY(bool ready READ ready NOTIFY readyChanged) Q_PROPERTY(bool hasSource READ hasSource NOTIFY readyChanged) Q_PROPERTY(bool hasValidCity READ hasValidCity NOTIFY cityChanged) Q_PROPERTY(bool hasValidWeather READ hasValidWeather NOTIFY weatherChanged) Q_PROPERTY(bool useGps READ useGps WRITE setUseGps NOTIFY useGpsChanged) Q_PROPERTY(QString city READ city WRITE setCity NOTIFY cityChanged) Q_PROPERTY(WeatherData *weather READ weather NOTIFY weatherChanged) Q_PROPERTY(QQmlListProperty<WeatherData> forecast READ forecast NOTIFY weatherChanged) QML_ELEMENT public: explicit AppModel(QObject *parent = 0); ~AppModel(); bool ready() const; bool hasSource() const; bool useGps() const; bool hasValidCity() const; bool hasValidWeather() const; void setUseGps(bool value); QString city() const; void setCity(const QString &value); WeatherData *weather() const; QQmlListProperty<WeatherData> forecast() const; public slots: Q_INVOKABLE void refreshWeather(); signals: void readyChanged(); void useGpsChanged(); void cityChanged(); void weatherChanged(); };
일기 예보 정보에는 다음 날의 일기 예보(일 수는 제공업체에 따라 다름)가 포함된 QQmlListProperty 를 사용합니다. 이렇게 하면 QML에서 일기 예보에 쉽게 액세스할 수 있습니다.
QML에 사용자 지정 모델 노출
모델을 QML UI 레이어에 노출하려면 QML_ELEMENT 및 QML_ANONYMOUS 매크로를 사용합니다. 이러한 매크로에 대한 자세한 내용은 QQmlEngine 클래스 설명을 참조하세요.
QML에서 유형을 사용할 수 있게 하려면 그에 따라 빌드를 업데이트해야 합니다.
CMake 빌드
CMake 기반 빌드의 경우 CMakeLists.txt
에 다음을 추가해야 합니다:
qt_add_qml_module(weatherinfo URI Weather VERSION 1.0 SOURCES appmodel.cpp appmodel.h openmeteobackend.cpp openmeteobackend.h openweathermapbackend.cpp openweathermapbackend.h providerbackend.cpp providerbackend.h weatherapibackend.cpp weatherapibackend.h QML_FILES BigForecastIcon.qml ForecastIcon.qml WeatherIcon.qml WeatherInfo.qml RESOURCES icons/weather-few-clouds.svg icons/weather-fog.svg icons/weather-haze.svg icons/weather-icy.svg icons/weather-overcast.svg icons/weather-showers.svg icons/weather-sleet.svg icons/weather-snow.svg icons/weather-storm.svg icons/weather-sunny-very-few-clouds.svg icons/weather-sunny.svg icons/weather-thundershower.svg icons/weather-showers-scattered.svg icons/waypoint.svg )
qmake 빌드
qmake 빌드의 경우 weatherinfo.pro
파일을 다음과 같이 수정해야 합니다:
CONFIG += qmltypes QML_IMPORT_NAME = Weather QML_IMPORT_MAJOR_VERSION = 1 qml_resources.files = \ qmldir \ BigForecastIcon.qml \ ForecastIcon.qml \ WeatherIcon.qml \ WeatherInfo.qml \ icons/weather-few-clouds.svg \ icons/weather-fog.svg \ icons/weather-haze.svg \ icons/weather-icy.svg \ icons/weather-overcast.svg \ icons/weather-showers.svg \ icons/weather-showers-scattered.svg \ icons/weather-sleet.svg \ icons/weather-snow.svg \ icons/weather-storm.svg \ icons/weather-sunny-very-few-clouds.svg \ icons/weather-sunny.svg \ icons/weather-thundershower.svg \ icons/waypoint.svg qml_resources.prefix = /qt/qml/Weather RESOURCES += qml_resources
QML에서 모델 인스턴스화
마지막으로, 실제 QML에서 AppModel
을 인스턴스화합니다:
Window { id: window AppModel { id: appModel onReadyChanged: { if (appModel.ready) statesItem.state = "ready" else statesItem.state = "loading" } } }
이렇게 모델이 인스턴스화되면 QML 문서의 다른 곳에서 해당 프로퍼티를 사용할 수 있습니다:
BigForecastIcon { id: current Layout.fillWidth: true Layout.fillHeight: true weatherIcon: (appModel.hasValidWeather ? appModel.weather.weatherIcon : "sunny") }
© 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.