WebEngine ウィジェット 地図の例
ジオロケーション リクエストの処理方法を示します。
Maps は、QWebEnginePage を起点とするジオロケーション リクエストの処理方法を示します。
ジオロケーション APIは JavaScript API で、ウェブアプリケーションがユーザーの物理的な位置を特定して地図上に表示するときなどに使用します。Qt WebEngine は Qt Positioningモジュールに依存しているため、ターゲットのプラットフォームで実行可能な位置情報バックエンドが必要です。
誤ってサードパーティに位置情報を送信しないように、ジオロケーションリクエストはデフォルトで拒否されます。この例では、アプリケーションがこれらのリクエストの受け付けを開始するために必要な手順を示します。
注: Windows 11では、アプリケーションにWindows位置情報サービスへのアクセスを許可する設定を有効にします。設定アプリのPrivacy & Security >Location で、Location services 、Let apps access your location 、Let desktop apps access your location を有効にします。
例の実行
からサンプルを実行するには Qt Creatorからサンプルを実行するには、Welcome モードを開き、Examples からサンプルを選択します。詳細については、Building and Running an Exampleを参照してください。
コード
このサンプル・プログラムは、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"))); }
Qt WebEngine HTML5 Geolocationも参照してください 。
© 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.