웹엔진 위젯 지도 예제

지리적 위치 요청을 처리하는 방법을 보여줍니다.

Maps는 QWebEnginePage 에서 시작된 지리적 위치 요청을 처리하는 방법을 보여줍니다.

지리적 위치 API는 웹 애플리케이션이 지도 등에 표시할 사용자의 실제 위치를 결정하는 데 사용할 수 있는 JavaScript API입니다. Qt WebEngine 은 이 API를 구동하기 위해 Qt Positioning 모듈에 의존하므로 대상 플랫폼에 실행 가능한 위치 백엔드가 필요합니다.

실수로 위치 정보를 타사에 전송하는 것을 방지하기 위해 기본적으로 위치 정보 요청은 거부됩니다. 이 예는 애플리케이션이 이러한 요청을 수락하기 위해 수행해야 하는 단계를 보여줍니다.

참고: Windows 11에서는 설정을 활성화하여 애플리케이션에 Windows 위치 서비스에 대한 액세스 권한을 부여합니다. 설정 앱의 Privacy & Security > Location 에서 Location services, Let apps access your locationLet desktop apps access your location 를 활성화합니다.

예제 실행하기

에서 예제를 실행하려면 Qt Creator에서 Welcome 모드를 열고 Examples 에서 예제를 선택합니다. 자세한 내용은 예제 빌드 및 실행하기를 참조하세요.

코드

예제 프로그램은 QMainWindow 에서 상속하는 단일 클래스 MainWindow 로 구성됩니다:

#include <QMainWindow>
#include <QWebEngineView>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);

private:
    QWebEngineView *m_view;
};

생성자에서 먼저 QWebEngineView 를 중앙 위젯으로 설정합니다:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , m_view(new QWebEngineView(this))
{
    setCentralWidget(m_view);

그런 다음 QWebEnginePage::permissionRequested 신호에 람다 함수를 연결합니다:

    QWebEnginePage *page = m_view->page();

    connect(page, &QWebEnginePage::permissionRequested,
            [this, page](QWebEnginePermission permission) {

이 신호는 웹 페이지에서 위치 서비스뿐만 아니라 오디오 캡처 장치나 마우스 잠금 등 특정 기능이나 장치를 사용하도록 요청할 때마다 발생합니다. 이 예에서는 위치 서비스 요청만 처리합니다:

        if (permission.permissionType() != QWebEnginePermission::PermissionType::Geolocation)
            return;

이제 실제로 사용자에게 권한을 요청하는 부분이 나옵니다:

        QMessageBox msgBox(this);
        msgBox.setText(tr("%1 wants to know your location").arg(permission.origin().host()));
        msgBox.setInformativeText(tr("Do you want to send your current location to this website?"));
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        msgBox.setDefaultButton(QMessageBox::Yes);

        if (msgBox.exec() == QMessageBox::Yes)
            permission.grant();
        else
            permission.deny();
    });

이 질문에는 사용자의 위치 데이터를 수신할 웹사이트를 정확히 알려주는 웹사이트의 URI(permission.origin())의 호스트 구성요소가 포함되어 있습니다.

QWebEnginePermission::grant() 및 QWebEnginePermission::deny() 메서드를 사용하여 사용자의 답변을 웹 페이지에 다시 전달합니다.

마지막으로 QWebEnginePage 에 위치 서비스를 사용할 수 있는 웹 페이지를 로드하도록 요청합니다:

    page->load(QUrl(QStringLiteral("https://bing.com/maps")));
}

예제 프로젝트 @ code.qt.io

Qt WebEngine HTML5 지오로케이션을참조하세요 .

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