diff options
Diffstat (limited to 'examples')
24 files changed, 238 insertions, 243 deletions
diff --git a/examples/pdfwidgets/pdfviewer/zoomselector.cpp b/examples/pdfwidgets/pdfviewer/zoomselector.cpp index 0205489aa..a4d596ab7 100644 --- a/examples/pdfwidgets/pdfviewer/zoomselector.cpp +++ b/examples/pdfwidgets/pdfviewer/zoomselector.cpp @@ -57,7 +57,7 @@ ZoomSelector::ZoomSelector(QWidget *parent) addItem(QLatin1String("200%")); addItem(QLatin1String("400%")); - connect(this, static_cast<void(QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged), + connect(this, &QComboBox::currentTextChanged, this, &ZoomSelector::onCurrentTextChanged); connect(lineEdit(), &QLineEdit::editingFinished, diff --git a/examples/webenginewidgets/cookiebrowser/mainwindow.cpp b/examples/webenginewidgets/cookiebrowser/mainwindow.cpp index 970381d4d..66e6988d8 100644 --- a/examples/webenginewidgets/cookiebrowser/mainwindow.cpp +++ b/examples/webenginewidgets/cookiebrowser/mainwindow.cpp @@ -87,7 +87,7 @@ QNetworkCookie CookieDialog::cookie() cookie.setDomain(m_domainLineEdit->text()); cookie.setName(m_nameLineEdit->text().toLatin1()); cookie.setValue(m_valueLineEdit->text().toLatin1()); - cookie.setExpirationDate(QDateTime(m_dateEdit->date())); + cookie.setExpirationDate(QDateTime(m_dateEdit->date(), QTime::currentTime())); cookie.setPath(m_pathLineEdit->text()); cookie.setSecure(m_isSecureComboBox->currentText() == tr("yes")); cookie.setHttpOnly(m_isHttpOnlyComboBox->currentText() == tr("yes")); diff --git a/examples/webenginewidgets/cookiebrowser/mainwindow.h b/examples/webenginewidgets/cookiebrowser/mainwindow.h index 643ff9322..b6aee1c24 100644 --- a/examples/webenginewidgets/cookiebrowser/mainwindow.h +++ b/examples/webenginewidgets/cookiebrowser/mainwindow.h @@ -98,7 +98,7 @@ private slots: private: QWebEngineCookieStore *m_store; - QVector<QNetworkCookie> m_cookies; + QList<QNetworkCookie> m_cookies; QVBoxLayout *m_layout; }; diff --git a/examples/webenginewidgets/printme/main.cpp b/examples/webenginewidgets/printme/main.cpp index e4d6d9dc8..3993c6740 100644 --- a/examples/webenginewidgets/printme/main.cpp +++ b/examples/webenginewidgets/printme/main.cpp @@ -64,10 +64,10 @@ int main(int argc, char *argv[]) view.show(); PrintHandler handler; - handler.setPage(view.page()); + handler.setView(&view); - auto printPreviewShortCut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_P), &view); - auto printShortCut = new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_P), &view); + auto printPreviewShortCut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_P), &view); + auto printShortCut = new QShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_P), &view); QObject::connect(printPreviewShortCut, &QShortcut::activated, &handler, &PrintHandler::printPreview); QObject::connect(printShortCut, &QShortcut::activated, &handler, &PrintHandler::print); diff --git a/examples/webenginewidgets/printme/printhandler.cpp b/examples/webenginewidgets/printme/printhandler.cpp index d8c4fc177..9d23c5b1f 100644 --- a/examples/webenginewidgets/printme/printhandler.cpp +++ b/examples/webenginewidgets/printme/printhandler.cpp @@ -54,7 +54,7 @@ #include <QPrinter> #include <QPainter> #include <QPrintPreviewDialog> -#include <QWebEnginePage> +#include <QWebEngineView> PrintHandler::PrintHandler(QObject *parent) : QObject(parent) @@ -62,17 +62,17 @@ PrintHandler::PrintHandler(QObject *parent) } -void PrintHandler::setPage(QWebEnginePage *page) +void PrintHandler::setView(QWebEngineView *view) { - Q_ASSERT(!m_page); - m_page = page; - connect(m_page, &QWebEnginePage::printRequested, this, &PrintHandler::printPreview); + Q_ASSERT(!m_view); + m_view = view; + connect(view->page(), &QWebEnginePage::printRequested, this, &PrintHandler::printPreview); } void PrintHandler::print() { QPrinter printer(QPrinter::HighResolution); - QPrintDialog dialog(&printer, m_page->view()); + QPrintDialog dialog(&printer, m_view); if (dialog.exec() != QDialog::Accepted) return; printDocument(&printer); @@ -83,7 +83,7 @@ void PrintHandler::printDocument(QPrinter *printer) QEventLoop loop; bool result; auto printPreview = [&](bool success) { result = success; loop.quit(); }; - m_page->print(printer, std::move(printPreview)); + m_view->page()->print(printer, std::move(printPreview)); loop.exec(); if (!result) { QPainter painter; @@ -101,13 +101,13 @@ void PrintHandler::printDocument(QPrinter *printer) void PrintHandler::printPreview() { - if (!m_page) + if (!m_view) return; if (m_inPrintPreview) return; m_inPrintPreview = true; QPrinter printer; - QPrintPreviewDialog preview(&printer, m_page->view()); + QPrintPreviewDialog preview(&printer, m_view); connect(&preview, &QPrintPreviewDialog::paintRequested, this, &PrintHandler::printDocument); preview.exec(); diff --git a/examples/webenginewidgets/printme/printhandler.h b/examples/webenginewidgets/printme/printhandler.h index 69b71f4a8..663fd73d6 100644 --- a/examples/webenginewidgets/printme/printhandler.h +++ b/examples/webenginewidgets/printme/printhandler.h @@ -56,7 +56,7 @@ QT_BEGIN_NAMESPACE class QPainter; class QPrinter; -class QWebEnginePage; +class QWebEngineView; QT_END_NAMESPACE class PrintHandler : public QObject @@ -64,7 +64,7 @@ class PrintHandler : public QObject Q_OBJECT public: PrintHandler(QObject *parent = nullptr); - void setPage(QWebEnginePage *page); + void setView(QWebEngineView *view); public slots: void print(); @@ -72,7 +72,7 @@ public slots: void printDocument(QPrinter *printer); private: - QWebEnginePage *m_page = nullptr; + QWebEngineView *m_view = nullptr; bool m_inPrintPreview = false; }; diff --git a/examples/webenginewidgets/simplebrowser/browser.h b/examples/webenginewidgets/simplebrowser/browser.h index 4c17121df..cb84ea670 100644 --- a/examples/webenginewidgets/simplebrowser/browser.h +++ b/examples/webenginewidgets/simplebrowser/browser.h @@ -53,7 +53,7 @@ #include "downloadmanagerwidget.h" -#include <QVector> +#include <QList> #include <QWebEngineProfile> class BrowserWindow; @@ -63,7 +63,7 @@ class Browser public: Browser(); - QVector<BrowserWindow*> windows() { return m_windows; } + QList<BrowserWindow*> windows() { return m_windows; } BrowserWindow *createWindow(bool offTheRecord = false); BrowserWindow *createDevToolsWindow(); @@ -71,7 +71,7 @@ public: DownloadManagerWidget &downloadManagerWidget() { return m_downloadManagerWidget; } private: - QVector<BrowserWindow*> m_windows; + QList<BrowserWindow*> m_windows; DownloadManagerWidget m_downloadManagerWidget; QScopedPointer<QWebEngineProfile> m_otrProfile; }; diff --git a/examples/webenginewidgets/simplebrowser/browserwindow.cpp b/examples/webenginewidgets/simplebrowser/browserwindow.cpp index 12a1e6844..26639117c 100644 --- a/examples/webenginewidgets/simplebrowser/browserwindow.cpp +++ b/examples/webenginewidgets/simplebrowser/browserwindow.cpp @@ -55,7 +55,6 @@ #include "webview.h" #include <QApplication> #include <QCloseEvent> -#include <QDesktopWidget> #include <QEvent> #include <QFileDialog> #include <QInputDialog> @@ -66,9 +65,7 @@ #include <QStatusBar> #include <QToolBar> #include <QVBoxLayout> -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) #include <QWebEngineFindTextResult> -#endif #include <QWebEngineProfile> BrowserWindow::BrowserWindow(Browser *browser, QWebEngineProfile *profile, bool forDevTools) @@ -132,9 +129,7 @@ BrowserWindow::BrowserWindow(Browser *browser, QWebEngineProfile *profile, bool connect(m_urlLineEdit, &QLineEdit::returnPressed, [this]() { m_tabWidget->setUrl(QUrl::fromUserInput(m_urlLineEdit->text())); }); -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) connect(m_tabWidget, &TabWidget::findTextFinished, this, &BrowserWindow::handleFindTextFinished); -#endif QAction *focusUrlLineEditAction = new QAction(this); addAction(focusUrlLineEditAction); @@ -316,7 +311,7 @@ QMenu *BrowserWindow::createWindowMenu(TabWidget *tabWidget) menu->addAction(previousTabAction); menu->addSeparator(); - QVector<BrowserWindow*> windows = m_browser->windows(); + QList<BrowserWindow*> windows = m_browser->windows(); int index(-1); for (auto window : windows) { QAction *action = menu->addAction(window->windowTitle(), this, &BrowserWindow::handleShowWindowTriggered); @@ -346,7 +341,7 @@ QToolBar *BrowserWindow::createToolBar() QList<QKeySequence> backShortcuts = QKeySequence::keyBindings(QKeySequence::Back); for (auto it = backShortcuts.begin(); it != backShortcuts.end();) { // Chromium already handles navigate on backspace when appropriate. - if ((*it)[0] == Qt::Key_Backspace) + if ((*it)[0].key() == Qt::Key_Backspace) it = backShortcuts.erase(it); else ++it; @@ -365,7 +360,7 @@ QToolBar *BrowserWindow::createToolBar() m_historyForwardAction = new QAction(this); QList<QKeySequence> fwdShortcuts = QKeySequence::keyBindings(QKeySequence::Forward); for (auto it = fwdShortcuts.begin(); it != fwdShortcuts.end();) { - if (((*it)[0] & Qt::Key_unknown) == Qt::Key_Backspace) + if (((*it)[0].key() & Qt::Key_unknown) == Qt::Key_Backspace) it = fwdShortcuts.erase(it); else ++it; @@ -466,14 +461,7 @@ void BrowserWindow::handleFindActionTriggered() m_lastSearch, &ok); if (ok && !search.isEmpty()) { m_lastSearch = search; -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) currentTab()->findText(m_lastSearch); -#else - currentTab()->findText(m_lastSearch, 0, [this](bool found) { - if (!found) - statusBar()->showMessage(tr("\"%1\" not found.").arg(m_lastSearch)); - }); -#endif } } @@ -525,7 +513,7 @@ void BrowserWindow::handleShowWindowTriggered() { if (QAction *action = qobject_cast<QAction*>(sender())) { int offset = action->data().toInt(); - QVector<BrowserWindow*> windows = m_browser->windows(); + QList<BrowserWindow*> windows = m_browser->windows(); windows.at(offset)->activateWindow(); windows.at(offset)->currentTab()->setFocus(); } @@ -537,7 +525,6 @@ void BrowserWindow::handleDevToolsRequested(QWebEnginePage *source) source->triggerAction(QWebEnginePage::InspectElement); } -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) void BrowserWindow::handleFindTextFinished(const QWebEngineFindTextResult &result) { if (result.numberOfMatches() == 0) { @@ -548,4 +535,3 @@ void BrowserWindow::handleFindTextFinished(const QWebEngineFindTextResult &resul QString::number(result.numberOfMatches()))); } } -#endif diff --git a/examples/webenginewidgets/simplebrowser/browserwindow.h b/examples/webenginewidgets/simplebrowser/browserwindow.h index ea12ad9df..11a655469 100644 --- a/examples/webenginewidgets/simplebrowser/browserwindow.h +++ b/examples/webenginewidgets/simplebrowser/browserwindow.h @@ -88,9 +88,7 @@ private slots: void handleWebViewTitleChanged(const QString &title); void handleWebActionEnabledChanged(QWebEnginePage::WebAction action, bool enabled); void handleDevToolsRequested(QWebEnginePage *source); -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) void handleFindTextFinished(const QWebEngineFindTextResult &result); -#endif private: QMenu *createFileMenu(TabWidget *tabWidget); diff --git a/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc b/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc index 251ca5ad8..2bbe3fe33 100644 --- a/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc +++ b/examples/webenginewidgets/simplebrowser/doc/src/simplebrowser.qdoc @@ -314,7 +314,7 @@ Downloads are associated with a \l QWebEngineProfile. Whenever a download is triggered on a web page the \l QWebEngineProfile::downloadRequested signal is - emitted with a \l QWebEngineDownloadItem, which in this example is forwarded + emitted with a \l QWebEngineDownloadRequest, which in this example is forwarded to \c DownloadManagerWidget::downloadRequested: \quotefromfile webenginewidgets/simplebrowser/browser.cpp @@ -329,10 +329,10 @@ \skipto DownloadManagerWidget::downloadRequested \printuntil /^\}/ - The \l QWebEngineDownloadItem object will periodically emit the \l - {QWebEngineDownloadItem::}{downloadProgress} signal to notify potential + The \l QWebEngineDownloadRequest object will periodically emit the \l + {QWebEngineDownloadRequest::}{downloadProgress} signal to notify potential observers of the download progress and the \l - {QWebEngineDownloadItem::}{stateChanged} signal when the download is + {QWebEngineDownloadRequest::}{stateChanged} signal when the download is finished or when an error occurs. See \c downloadmanagerwidget.cpp for an example of how these signals can be handled. diff --git a/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.cpp b/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.cpp index 2c8831429..cdd3a414a 100644 --- a/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.cpp +++ b/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.cpp @@ -56,7 +56,7 @@ #include <QFileDialog> #include <QDir> -#include <QWebEngineDownloadItem> +#include <QWebEngineDownloadRequest> DownloadManagerWidget::DownloadManagerWidget(QWidget *parent) : QWidget(parent) @@ -65,24 +65,16 @@ DownloadManagerWidget::DownloadManagerWidget(QWidget *parent) setupUi(this); } -void DownloadManagerWidget::downloadRequested(QWebEngineDownloadItem *download) +void DownloadManagerWidget::downloadRequested(QWebEngineDownloadRequest *download) { - Q_ASSERT(download && download->state() == QWebEngineDownloadItem::DownloadRequested); + Q_ASSERT(download && download->state() == QWebEngineDownloadRequest::DownloadRequested); -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) QString path = QFileDialog::getSaveFileName(this, tr("Save as"), QDir(download->downloadDirectory()).filePath(download->downloadFileName())); -#else - QString path = QFileDialog::getSaveFileName(this, tr("Save as"), download->path()); -#endif if (path.isEmpty()) return; -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) download->setDownloadDirectory(QFileInfo(path).path()); download->setDownloadFileName(QFileInfo(path).fileName()); -#else - download->setPath(path); -#endif download->accept(); add(new DownloadWidget(download)); diff --git a/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.h b/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.h index 7be9d8102..221cd261f 100644 --- a/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.h +++ b/examples/webenginewidgets/simplebrowser/downloadmanagerwidget.h @@ -56,7 +56,7 @@ #include <QWidget> QT_BEGIN_NAMESPACE -class QWebEngineDownloadItem; +class QWebEngineDownloadRequest; QT_END_NAMESPACE class DownloadWidget; @@ -69,9 +69,9 @@ public: explicit DownloadManagerWidget(QWidget *parent = nullptr); // Prompts user with a "Save As" dialog. If the user doesn't cancel it, then - // the QWebEngineDownloadItem will be accepted and the DownloadManagerWidget + // the QWebEngineDownloadRequest will be accepted and the DownloadManagerWidget // will be shown on the screen. - void downloadRequested(QWebEngineDownloadItem *webItem); + void downloadRequested(QWebEngineDownloadRequest *webItem); private: void add(DownloadWidget *downloadWidget); diff --git a/examples/webenginewidgets/simplebrowser/downloadwidget.cpp b/examples/webenginewidgets/simplebrowser/downloadwidget.cpp index c4e3cd805..e9caa8658 100644 --- a/examples/webenginewidgets/simplebrowser/downloadwidget.cpp +++ b/examples/webenginewidgets/simplebrowser/downloadwidget.cpp @@ -52,34 +52,30 @@ #include <QFileInfo> #include <QUrl> -#include <QWebEngineDownloadItem> +#include <QWebEngineDownloadRequest> -DownloadWidget::DownloadWidget(QWebEngineDownloadItem *download, QWidget *parent) +DownloadWidget::DownloadWidget(QWebEngineDownloadRequest *download, QWidget *parent) : QFrame(parent) , m_download(download) , m_timeAdded() { m_timeAdded.start(); setupUi(this); -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) m_dstName->setText(m_download->downloadFileName()); -#else - m_dstName->setText(QFileInfo(m_download->path()).fileName()); -#endif m_srcUrl->setText(m_download->url().toDisplayString()); connect(m_cancelButton, &QPushButton::clicked, [this](bool) { - if (m_download->state() == QWebEngineDownloadItem::DownloadInProgress) + if (m_download->state() == QWebEngineDownloadRequest::DownloadInProgress) m_download->cancel(); else emit removeClicked(this); }); - connect(m_download, &QWebEngineDownloadItem::downloadProgress, + connect(m_download, &QWebEngineDownloadRequest::downloadProgress, this, &DownloadWidget::updateWidget); - connect(m_download, &QWebEngineDownloadItem::stateChanged, + connect(m_download, &QWebEngineDownloadRequest::stateChanged, this, &DownloadWidget::updateWidget); updateWidget(); @@ -105,10 +101,10 @@ void DownloadWidget::updateWidget() auto state = m_download->state(); switch (state) { - case QWebEngineDownloadItem::DownloadRequested: + case QWebEngineDownloadRequest::DownloadRequested: Q_UNREACHABLE(); break; - case QWebEngineDownloadItem::DownloadInProgress: + case QWebEngineDownloadRequest::DownloadInProgress: if (totalBytes >= 0) { m_progressBar->setValue(qRound(100 * receivedBytes / totalBytes)); m_progressBar->setDisabled(false); @@ -126,7 +122,7 @@ void DownloadWidget::updateWidget() .arg(withUnit(bytesPerSecond))); } break; - case QWebEngineDownloadItem::DownloadCompleted: + case QWebEngineDownloadRequest::DownloadCompleted: m_progressBar->setValue(100); m_progressBar->setDisabled(true); m_progressBar->setFormat( @@ -134,7 +130,7 @@ void DownloadWidget::updateWidget() .arg(withUnit(receivedBytes)) .arg(withUnit(bytesPerSecond))); break; - case QWebEngineDownloadItem::DownloadCancelled: + case QWebEngineDownloadRequest::DownloadCancelled: m_progressBar->setValue(0); m_progressBar->setDisabled(true); m_progressBar->setFormat( @@ -142,7 +138,7 @@ void DownloadWidget::updateWidget() .arg(withUnit(receivedBytes)) .arg(withUnit(bytesPerSecond))); break; - case QWebEngineDownloadItem::DownloadInterrupted: + case QWebEngineDownloadRequest::DownloadInterrupted: m_progressBar->setValue(0); m_progressBar->setDisabled(true); m_progressBar->setFormat( @@ -151,7 +147,7 @@ void DownloadWidget::updateWidget() break; } - if (state == QWebEngineDownloadItem::DownloadInProgress) { + if (state == QWebEngineDownloadRequest::DownloadInProgress) { static QIcon cancelIcon(QStringLiteral(":process-stop.png")); m_cancelButton->setIcon(cancelIcon); m_cancelButton->setToolTip(tr("Stop downloading")); diff --git a/examples/webenginewidgets/simplebrowser/downloadwidget.h b/examples/webenginewidgets/simplebrowser/downloadwidget.h index c20676aa6..c59b1b4b7 100644 --- a/examples/webenginewidgets/simplebrowser/downloadwidget.h +++ b/examples/webenginewidgets/simplebrowser/downloadwidget.h @@ -57,16 +57,16 @@ #include <QElapsedTimer> QT_BEGIN_NAMESPACE -class QWebEngineDownloadItem; +class QWebEngineDownloadRequest; QT_END_NAMESPACE -// Displays one ongoing or finished download (QWebEngineDownloadItem). +// Displays one ongoing or finished download (QWebEngineDownloadRequest). class DownloadWidget final : public QFrame, public Ui::DownloadWidget { Q_OBJECT public: - // Precondition: The QWebEngineDownloadItem has been accepted. - explicit DownloadWidget(QWebEngineDownloadItem *download, QWidget *parent = nullptr); + // Precondition: The QWebEngineDownloadRequest has been accepted. + explicit DownloadWidget(QWebEngineDownloadRequest *download, QWidget *parent = nullptr); signals: // This signal is emitted when the user indicates that they want to remove @@ -79,7 +79,7 @@ private slots: private: QString withUnit(qreal bytes); - QWebEngineDownloadItem *m_download; + QWebEngineDownloadRequest *m_download; QElapsedTimer m_timeAdded; }; diff --git a/examples/webenginewidgets/simplebrowser/main.cpp b/examples/webenginewidgets/simplebrowser/main.cpp index ddc8b4d01..543742b33 100644 --- a/examples/webenginewidgets/simplebrowser/main.cpp +++ b/examples/webenginewidgets/simplebrowser/main.cpp @@ -74,11 +74,9 @@ int main(int argc, char **argv) QApplication app(argc, argv); app.setWindowIcon(QIcon(QStringLiteral(":AppLogoColor.png"))); - QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::PluginsEnabled, true); -#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0) - QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::DnsPrefetchEnabled, true); + QWebEngineProfile::defaultProfile()->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true); + QWebEngineProfile::defaultProfile()->settings()->setAttribute(QWebEngineSettings::DnsPrefetchEnabled, true); QWebEngineProfile::defaultProfile()->setUseForGlobalCertificateVerification(); -#endif QUrl url = commandLineUrlArgument(); diff --git a/examples/webenginewidgets/simplebrowser/tabwidget.cpp b/examples/webenginewidgets/simplebrowser/tabwidget.cpp index 10ccdad59..b706a3ab7 100644 --- a/examples/webenginewidgets/simplebrowser/tabwidget.cpp +++ b/examples/webenginewidgets/simplebrowser/tabwidget.cpp @@ -82,7 +82,7 @@ TabWidget::TabWidget(QWebEngineProfile *profile, QWidget *parent) QPixmap pixmap(QStringLiteral(":ninja.png")); icon->setPixmap(pixmap.scaledToHeight(tabBar->height())); setStyleSheet(QStringLiteral("QTabWidget::tab-bar { left: %1px; }"). - arg(icon->pixmap()->width())); + arg(icon->pixmap().width())); } } @@ -202,12 +202,10 @@ void TabWidget::setupView(WebView *webView) closeTab(index); }); connect(webView, &WebView::devToolsRequested, this, &TabWidget::devToolsRequested); -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) connect(webPage, &QWebEnginePage::findTextFinished, [this, webView](const QWebEngineFindTextResult &result) { if (currentIndex() == indexOf(webView)) emit findTextFinished(result); }); -#endif } WebView *TabWidget::createTab() diff --git a/examples/webenginewidgets/simplebrowser/tabwidget.h b/examples/webenginewidgets/simplebrowser/tabwidget.h index bf65c9505..06f3db829 100644 --- a/examples/webenginewidgets/simplebrowser/tabwidget.h +++ b/examples/webenginewidgets/simplebrowser/tabwidget.h @@ -52,6 +52,7 @@ #define TABWIDGET_H #include <QTabWidget> +#include <QWebEngineFindTextResult> #include <QWebEnginePage> QT_BEGIN_NAMESPACE @@ -78,9 +79,7 @@ signals: void favIconChanged(const QIcon &icon); void webActionEnabledChanged(QWebEnginePage::WebAction action, bool enabled); void devToolsRequested(QWebEnginePage *source); -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) void findTextFinished(const QWebEngineFindTextResult &result); -#endif public slots: // current tab/page slots diff --git a/examples/webenginewidgets/simplebrowser/webpage.cpp b/examples/webenginewidgets/simplebrowser/webpage.cpp index 9f7038cdb..4db147526 100644 --- a/examples/webenginewidgets/simplebrowser/webpage.cpp +++ b/examples/webenginewidgets/simplebrowser/webpage.cpp @@ -50,98 +50,22 @@ #include "browserwindow.h" #include "tabwidget.h" -#include "ui_certificateerrordialog.h" -#include "ui_passworddialog.h" + #include "webpage.h" #include "webview.h" -#include <QAuthenticator> -#include <QMessageBox> -#include <QStyle> #include <QTimer> -#include <QWebEngineCertificateError> WebPage::WebPage(QWebEngineProfile *profile, QObject *parent) : QWebEnginePage(profile, parent) { - connect(this, &QWebEnginePage::authenticationRequired, this, &WebPage::handleAuthenticationRequired); - connect(this, &QWebEnginePage::featurePermissionRequested, this, &WebPage::handleFeaturePermissionRequested); - connect(this, &QWebEnginePage::proxyAuthenticationRequired, this, &WebPage::handleProxyAuthenticationRequired); - connect(this, &QWebEnginePage::registerProtocolHandlerRequested, this, &WebPage::handleRegisterProtocolHandlerRequested); -#if !defined(QT_NO_SSL) || QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) connect(this, &QWebEnginePage::selectClientCertificate, this, &WebPage::handleSelectClientCertificate); -#endif } -bool WebPage::certificateError(const QWebEngineCertificateError &error) +void WebPage::certificateError(QWebEngineCertificateError error) { - QWidget *mainWindow = view()->window(); - -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - QWebEngineCertificateError deferredError = error; - deferredError.defer(); - - QTimer::singleShot(0, mainWindow, [mainWindow, error = std::move(deferredError)] () mutable { - if (!error.deferred()) { - QMessageBox::critical(mainWindow, tr("Certificate Error"), error.errorDescription()); - } else { -#else - if (error.isOverridable()) { -#endif - QDialog dialog(mainWindow); - dialog.setModal(true); - dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint); - - Ui::CertificateErrorDialog certificateDialog; - certificateDialog.setupUi(&dialog); - certificateDialog.m_iconLabel->setText(QString()); - QIcon icon(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxWarning, 0, mainWindow)); - certificateDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32)); - certificateDialog.m_errorLabel->setText(error.errorDescription()); - dialog.setWindowTitle(tr("Certificate Error")); - -#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) - if (dialog.exec() == QDialog::Accepted) - error.ignoreCertificateError(); - else - error.rejectCertificate(); - } - }); - return true; -#else - return dialog.exec() == QDialog::Accepted; - } - - QMessageBox::critical(mainWindow, tr("Certificate Error"), error.errorDescription()); - return false; -#endif -} - -void WebPage::handleAuthenticationRequired(const QUrl &requestUrl, QAuthenticator *auth) -{ - QWidget *mainWindow = view()->window(); - QDialog dialog(mainWindow); - dialog.setModal(true); - dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint); - - Ui::PasswordDialog passwordDialog; - passwordDialog.setupUi(&dialog); - - passwordDialog.m_iconLabel->setText(QString()); - QIcon icon(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow)); - passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32)); - - QString introMessage(tr("Enter username and password for \"%1\" at %2") - .arg(auth->realm()).arg(requestUrl.toString().toHtmlEscaped())); - passwordDialog.m_infoLabel->setText(introMessage); - passwordDialog.m_infoLabel->setWordWrap(true); - - if (dialog.exec() == QDialog::Accepted) { - auth->setUser(passwordDialog.m_userNameLineEdit->text()); - auth->setPassword(passwordDialog.m_passwordLineEdit->text()); - } else { - // Set authenticator null if dialog is cancelled - *auth = QAuthenticator(); - } + error.defer(); + QTimer::singleShot(0, this, + [this, error]() mutable { emit createCertificateErrorDialog(error); }); } inline QString questionForFeature(QWebEnginePage::Feature feature) @@ -167,64 +91,8 @@ inline QString questionForFeature(QWebEnginePage::Feature feature) return QString(); } -void WebPage::handleFeaturePermissionRequested(const QUrl &securityOrigin, Feature feature) -{ - QString title = tr("Permission Request"); - QString question = questionForFeature(feature).arg(securityOrigin.host()); - if (!question.isEmpty() && QMessageBox::question(view()->window(), title, question) == QMessageBox::Yes) - setFeaturePermission(securityOrigin, feature, PermissionGrantedByUser); - else - setFeaturePermission(securityOrigin, feature, PermissionDeniedByUser); -} - -void WebPage::handleProxyAuthenticationRequired(const QUrl &, QAuthenticator *auth, const QString &proxyHost) -{ - QWidget *mainWindow = view()->window(); - QDialog dialog(mainWindow); - dialog.setModal(true); - dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint); - - Ui::PasswordDialog passwordDialog; - passwordDialog.setupUi(&dialog); - - passwordDialog.m_iconLabel->setText(QString()); - QIcon icon(mainWindow->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, mainWindow)); - passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32)); - - QString introMessage = tr("Connect to proxy \"%1\" using:"); - introMessage = introMessage.arg(proxyHost.toHtmlEscaped()); - passwordDialog.m_infoLabel->setText(introMessage); - passwordDialog.m_infoLabel->setWordWrap(true); - - if (dialog.exec() == QDialog::Accepted) { - auth->setUser(passwordDialog.m_userNameLineEdit->text()); - auth->setPassword(passwordDialog.m_passwordLineEdit->text()); - } else { - // Set authenticator null if dialog is cancelled - *auth = QAuthenticator(); - } -} - -//! [registerProtocolHandlerRequested] -void WebPage::handleRegisterProtocolHandlerRequested(QWebEngineRegisterProtocolHandlerRequest request) -{ - auto answer = QMessageBox::question( - view()->window(), - tr("Permission Request"), - tr("Allow %1 to open all %2 links?") - .arg(request.origin().host()) - .arg(request.scheme())); - if (answer == QMessageBox::Yes) - request.accept(); - else - request.reject(); -} -//! [registerProtocolHandlerRequested] - -#if !defined(QT_NO_SSL) || QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) void WebPage::handleSelectClientCertificate(QWebEngineClientCertificateSelection selection) { // Just select one. selection.select(selection.certificates().at(0)); } -#endif diff --git a/examples/webenginewidgets/simplebrowser/webpage.h b/examples/webenginewidgets/simplebrowser/webpage.h index 908295894..00eb48156 100644 --- a/examples/webenginewidgets/simplebrowser/webpage.h +++ b/examples/webenginewidgets/simplebrowser/webpage.h @@ -53,6 +53,7 @@ #include <QWebEnginePage> #include <QWebEngineRegisterProtocolHandlerRequest> +#include <QWebEngineCertificateError> class WebPage : public QWebEnginePage { @@ -61,17 +62,14 @@ class WebPage : public QWebEnginePage public: WebPage(QWebEngineProfile *profile, QObject *parent = nullptr); +signals: + void createCertificateErrorDialog(QWebEngineCertificateError error); + protected: - bool certificateError(const QWebEngineCertificateError &error) override; + void certificateError(QWebEngineCertificateError error) override; private slots: - void handleAuthenticationRequired(const QUrl &requestUrl, QAuthenticator *auth); - void handleFeaturePermissionRequested(const QUrl &securityOrigin, Feature feature); - void handleProxyAuthenticationRequired(const QUrl &requestUrl, QAuthenticator *auth, const QString &proxyHost); - void handleRegisterProtocolHandlerRequested(QWebEngineRegisterProtocolHandlerRequest request); -#if !defined(QT_NO_SSL) || QT_VERSION >= QT_VERSION_CHECK(5, 12, 0) void handleSelectClientCertificate(QWebEngineClientCertificateSelection clientCertSelection); -#endif }; #endif // WEBPAGE_H diff --git a/examples/webenginewidgets/simplebrowser/webview.cpp b/examples/webenginewidgets/simplebrowser/webview.cpp index fcbb543f2..5bd0a5ef4 100644 --- a/examples/webenginewidgets/simplebrowser/webview.cpp +++ b/examples/webenginewidgets/simplebrowser/webview.cpp @@ -54,11 +54,15 @@ #include "webpage.h" #include "webpopupwindow.h" #include "webview.h" +#include "ui_certificateerrordialog.h" +#include "ui_passworddialog.h" #include <QContextMenuEvent> #include <QDebug> #include <QMenu> #include <QMessageBox> +#include <QAuthenticator> #include <QTimer> +#include <QStyle> WebView::WebView(QWidget *parent) : QWebEngineView(parent) @@ -104,13 +108,56 @@ WebView::WebView(QWidget *parent) }); } +inline QString questionForFeature(QWebEnginePage::Feature feature) +{ + switch (feature) { + case QWebEnginePage::Geolocation: + return QObject::tr("Allow %1 to access your location information?"); + case QWebEnginePage::MediaAudioCapture: + return QObject::tr("Allow %1 to access your microphone?"); + case QWebEnginePage::MediaVideoCapture: + return QObject::tr("Allow %1 to access your webcam?"); + case QWebEnginePage::MediaAudioVideoCapture: + return QObject::tr("Allow %1 to access your microphone and webcam?"); + case QWebEnginePage::MouseLock: + return QObject::tr("Allow %1 to lock your mouse cursor?"); + case QWebEnginePage::DesktopVideoCapture: + return QObject::tr("Allow %1 to capture video of your desktop?"); + case QWebEnginePage::DesktopAudioVideoCapture: + return QObject::tr("Allow %1 to capture audio and video of your desktop?"); + case QWebEnginePage::Notifications: + return QObject::tr("Allow %1 to show notification on your desktop?"); + } + return QString(); +} + void WebView::setPage(WebPage *page) { + WebPage *oldPage = qobject_cast<WebPage *>(QWebEngineView::page()); + disconnect(oldPage, &WebPage::createCertificateErrorDialog, this, + &WebView::handleCertificateError); + disconnect(oldPage, &QWebEnginePage::authenticationRequired, this, + &WebView::handleAuthenticationRequired); + disconnect(oldPage, &QWebEnginePage::featurePermissionRequested, this, + &WebView::handleFeaturePermissionRequested); + disconnect(oldPage, &QWebEnginePage::proxyAuthenticationRequired, this, + &WebView::handleProxyAuthenticationRequired); + disconnect(oldPage, &QWebEnginePage::registerProtocolHandlerRequested, this, + &WebView::handleRegisterProtocolHandlerRequested); createWebActionTrigger(page,QWebEnginePage::Forward); createWebActionTrigger(page,QWebEnginePage::Back); createWebActionTrigger(page,QWebEnginePage::Reload); createWebActionTrigger(page,QWebEnginePage::Stop); QWebEngineView::setPage(page); + connect(page, &WebPage::createCertificateErrorDialog, this, &WebView::handleCertificateError); + connect(page, &QWebEnginePage::authenticationRequired, this, + &WebView::handleAuthenticationRequired); + connect(page, &QWebEnginePage::featurePermissionRequested, this, + &WebView::handleFeaturePermissionRequested); + connect(page, &QWebEnginePage::proxyAuthenticationRequired, this, + &WebView::handleProxyAuthenticationRequired); + connect(page, &QWebEnginePage::registerProtocolHandlerRequested, this, + &WebView::handleRegisterProtocolHandlerRequested); } int WebView::loadProgress() const @@ -176,7 +223,7 @@ QWebEngineView *WebView::createWindow(QWebEnginePage::WebWindowType type) void WebView::contextMenuEvent(QContextMenuEvent *event) { - QMenu *menu = page()->createStandardContextMenu(); + QMenu *menu = createStandardContextMenu(); const QList<QAction *> actions = menu->actions(); auto inspectElement = std::find(actions.cbegin(), actions.cend(), page()->action(QWebEnginePage::InspectElement)); if (inspectElement == actions.cend()) { @@ -196,3 +243,106 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) menu->popup(event->globalPos()); } +void WebView::handleCertificateError(QWebEngineCertificateError error) +{ + QDialog dialog(window()); + dialog.setModal(true); + dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint); + + Ui::CertificateErrorDialog certificateDialog; + certificateDialog.setupUi(&dialog); + certificateDialog.m_iconLabel->setText(QString()); + QIcon icon(window()->style()->standardIcon(QStyle::SP_MessageBoxWarning, 0, window())); + certificateDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32)); + certificateDialog.m_errorLabel->setText(error.description()); + dialog.setWindowTitle(tr("Certificate Error")); + + if (dialog.exec() == QDialog::Accepted) + error.acceptCertificate(); + else + error.rejectCertificate(); +} + +void WebView::handleAuthenticationRequired(const QUrl &requestUrl, QAuthenticator *auth) +{ + QDialog dialog(window()); + dialog.setModal(true); + dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint); + + Ui::PasswordDialog passwordDialog; + passwordDialog.setupUi(&dialog); + + passwordDialog.m_iconLabel->setText(QString()); + QIcon icon(window()->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, window())); + passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32)); + + QString introMessage(tr("Enter username and password for \"%1\" at %2") + .arg(auth->realm()) + .arg(requestUrl.toString().toHtmlEscaped())); + passwordDialog.m_infoLabel->setText(introMessage); + passwordDialog.m_infoLabel->setWordWrap(true); + + if (dialog.exec() == QDialog::Accepted) { + auth->setUser(passwordDialog.m_userNameLineEdit->text()); + auth->setPassword(passwordDialog.m_passwordLineEdit->text()); + } else { + // Set authenticator null if dialog is cancelled + *auth = QAuthenticator(); + } +} + +void WebView::handleFeaturePermissionRequested(const QUrl &securityOrigin, + QWebEnginePage::Feature feature) +{ + QString title = tr("Permission Request"); + QString question = questionForFeature(feature).arg(securityOrigin.host()); + if (!question.isEmpty() && QMessageBox::question(window(), title, question) == QMessageBox::Yes) + page()->setFeaturePermission(securityOrigin, feature, + QWebEnginePage::PermissionGrantedByUser); + else + page()->setFeaturePermission(securityOrigin, feature, + QWebEnginePage::PermissionDeniedByUser); +} + +void WebView::handleProxyAuthenticationRequired(const QUrl &, QAuthenticator *auth, + const QString &proxyHost) +{ + QDialog dialog(window()); + dialog.setModal(true); + dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint); + + Ui::PasswordDialog passwordDialog; + passwordDialog.setupUi(&dialog); + + passwordDialog.m_iconLabel->setText(QString()); + QIcon icon(window()->style()->standardIcon(QStyle::SP_MessageBoxQuestion, 0, window())); + passwordDialog.m_iconLabel->setPixmap(icon.pixmap(32, 32)); + + QString introMessage = tr("Connect to proxy \"%1\" using:"); + introMessage = introMessage.arg(proxyHost.toHtmlEscaped()); + passwordDialog.m_infoLabel->setText(introMessage); + passwordDialog.m_infoLabel->setWordWrap(true); + + if (dialog.exec() == QDialog::Accepted) { + auth->setUser(passwordDialog.m_userNameLineEdit->text()); + auth->setPassword(passwordDialog.m_passwordLineEdit->text()); + } else { + // Set authenticator null if dialog is cancelled + *auth = QAuthenticator(); + } +} + +//! [registerProtocolHandlerRequested] +void WebView::handleRegisterProtocolHandlerRequested( + QWebEngineRegisterProtocolHandlerRequest request) +{ + auto answer = QMessageBox::question(window(), tr("Permission Request"), + tr("Allow %1 to open all %2 links?") + .arg(request.origin().host()) + .arg(request.scheme())); + if (answer == QMessageBox::Yes) + request.accept(); + else + request.reject(); +} +//! [registerProtocolHandlerRequested] diff --git a/examples/webenginewidgets/simplebrowser/webview.h b/examples/webenginewidgets/simplebrowser/webview.h index 8559a68b8..0dc7c33ad 100644 --- a/examples/webenginewidgets/simplebrowser/webview.h +++ b/examples/webenginewidgets/simplebrowser/webview.h @@ -53,6 +53,9 @@ #include <QIcon> #include <QWebEngineView> +#include <QWebEngineCertificateError> +#include <QWebEnginePage> +#include <QWebEngineRegisterProtocolHandlerRequest> class WebPage; @@ -76,6 +79,14 @@ signals: void webActionEnabledChanged(QWebEnginePage::WebAction webAction, bool enabled); void favIconChanged(const QIcon &icon); void devToolsRequested(QWebEnginePage *source); +private slots: + void handleCertificateError(QWebEngineCertificateError error); + void handleAuthenticationRequired(const QUrl &requestUrl, QAuthenticator *auth); + void handleFeaturePermissionRequested(const QUrl &securityOrigin, + QWebEnginePage::Feature feature); + void handleProxyAuthenticationRequired(const QUrl &requestUrl, QAuthenticator *auth, + const QString &proxyHost); + void handleRegisterProtocolHandlerRequested(QWebEngineRegisterProtocolHandlerRequest request); private: void createWebActionTrigger(QWebEnginePage *page, QWebEnginePage::WebAction); diff --git a/examples/webenginewidgets/spellchecker/webview.cpp b/examples/webenginewidgets/spellchecker/webview.cpp index a65fb1a1a..b532dd417 100644 --- a/examples/webenginewidgets/spellchecker/webview.cpp +++ b/examples/webenginewidgets/spellchecker/webview.cpp @@ -52,7 +52,7 @@ #include <QContextMenuEvent> #include <QMenu> #include <QWebEngineProfile> -#include <QWebEngineContextMenuData> +#include <QWebEngineContextMenuRequest> WebView::WebView(QWidget *parent) : QWebEngineView(parent) @@ -66,17 +66,17 @@ WebView::WebView(QWidget *parent) void WebView::contextMenuEvent(QContextMenuEvent *event) { - const QWebEngineContextMenuData &data = page()->contextMenuData(); - Q_ASSERT(data.isValid()); + QWebEngineContextMenuRequest *data = lastContextMenuRequest(); + Q_ASSERT(data); - if (!data.isContentEditable()) { + if (!data->isContentEditable()) { QWebEngineView::contextMenuEvent(event); return; } QWebEngineProfile *profile = page()->profile(); const QStringList &languages = profile->spellCheckLanguages(); - QMenu *menu = page()->createStandardContextMenu(); + QMenu *menu = createStandardContextMenu(); menu->addSeparator(); QAction *spellcheckAction = new QAction(tr("Check Spelling"), nullptr); diff --git a/examples/webenginewidgets/stylesheetbrowser/main.cpp b/examples/webenginewidgets/stylesheetbrowser/main.cpp index 833ea6bb0..9cd294d48 100644 --- a/examples/webenginewidgets/stylesheetbrowser/main.cpp +++ b/examples/webenginewidgets/stylesheetbrowser/main.cpp @@ -55,7 +55,7 @@ int main(int argc, char *argv[]) { - qRegisterMetaTypeStreamOperators<StyleSheet>("StyleSheet"); + qRegisterMetaType<StyleSheet>("StyleSheet"); QCoreApplication::setOrganizationName("QtExamples"); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); diff --git a/examples/webenginewidgets/stylesheetbrowser/mainwindow.cpp b/examples/webenginewidgets/stylesheetbrowser/mainwindow.cpp index c93205b18..bdf3c56bf 100644 --- a/examples/webenginewidgets/stylesheetbrowser/mainwindow.cpp +++ b/examples/webenginewidgets/stylesheetbrowser/mainwindow.cpp @@ -121,14 +121,15 @@ void MainWindow::removeStyleSheet(const QString &name, bool immediately) if (immediately) ui->webEngineView->page()->runJavaScript(s, QWebEngineScript::ApplicationWorld); - QWebEngineScript script = ui->webEngineView->page()->scripts().findScript(name); - ui->webEngineView->page()->scripts().remove(script); + const QList<QWebEngineScript> scripts = ui->webEngineView->page()->scripts().find(name); + if (!scripts.isEmpty()) + ui->webEngineView->page()->scripts().remove(scripts.first()); } bool MainWindow::hasStyleSheet(const QString &name) { - QWebEngineScript script = ui->webEngineView->page()->scripts().findScript(name); - return !script.isNull(); + const QList<QWebEngineScript> scripts = ui->webEngineView->page()->scripts().find(name); + return !scripts.isEmpty(); } void MainWindow::loadDefaultStyleSheets() |