From 4f7f139badd6d928aa053afa04d94e256ac52f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCri=20Valdmann?= Date: Fri, 2 Jun 2017 10:42:55 +0200 Subject: Update Content Manipulation example code - Use the new signals and slots syntax, QStringLiteral, lambdas and nullptr. - Use tr consistently for user-visible text. - Use unprefixed CSS transform properties. - Remove the "; undefined" workaround for runJavaScript calls, which is supposed to prevent a "possible recursion loop and crash" due to QVariant conversion. However, since no callback is passed to the method, no such conversion is taking place nowadays. Task-number: QTBUG-60655 Change-Id: Iad9131a17e488bf5e6b1905483be33b562dbfc62 Reviewed-by: Michal Klocek Reviewed-by: Viktor Engelmann --- .../doc/src/contentmanipulation.qdoc | 6 +- .../contentmanipulation/mainwindow.cpp | 64 +++++++++------------- 2 files changed, 26 insertions(+), 44 deletions(-) (limited to 'examples/webenginewidgets') diff --git a/examples/webenginewidgets/contentmanipulation/doc/src/contentmanipulation.qdoc b/examples/webenginewidgets/contentmanipulation/doc/src/contentmanipulation.qdoc index 685c20abc..66a1252dc 100644 --- a/examples/webenginewidgets/contentmanipulation/doc/src/contentmanipulation.qdoc +++ b/examples/webenginewidgets/contentmanipulation/doc/src/contentmanipulation.qdoc @@ -74,7 +74,7 @@ The second part of the constructor creates a QWebEngineView and connects slots to the view's signals: - \printuntil SLOT(finishLoading + \printuntil MainWindow::finishLoading Furthermore, we create a QLineEdit as the browser's address bar. We then set the vertical QSizePolicy to fill the available area in the browser at all times. We add the @@ -136,10 +136,6 @@ \printuntil } \printuntil } - We append \c undefined after the jQuery call to prevent a possible recursion loop - and crash caused by the way the elements returned by the each iterator elements - reference each other, which causes problems upon converting them to QVariant. - The \c rotateImages() function rotates the images on the current web page. This JavaScript code relies on CSS transforms. It looks up all \e {img} elements and rotates the images 180 degrees diff --git a/examples/webenginewidgets/contentmanipulation/mainwindow.cpp b/examples/webenginewidgets/contentmanipulation/mainwindow.cpp index 74d647c69..154a37747 100644 --- a/examples/webenginewidgets/contentmanipulation/mainwindow.cpp +++ b/examples/webenginewidgets/contentmanipulation/mainwindow.cpp @@ -52,22 +52,6 @@ #include #include "mainwindow.h" -template -struct InvokeWrapper { - R *receiver; - void (C::*memberFun)(Arg); - void operator()(Arg result) { - (receiver->*memberFun)(result); - } -}; - -template -InvokeWrapper invoke(R *receiver, void (C::*memberFun)(Arg)) -{ - InvokeWrapper wrapper = {receiver, memberFun}; - return wrapper; -} - MainWindow::MainWindow(const QUrl& url) { setAttribute(Qt::WA_DeleteOnClose, true); @@ -82,14 +66,14 @@ MainWindow::MainWindow(const QUrl& url) view = new QWebEngineView(this); view->load(url); - connect(view, SIGNAL(loadFinished(bool)), SLOT(adjustLocation())); - connect(view, SIGNAL(titleChanged(QString)), SLOT(adjustTitle())); - connect(view, SIGNAL(loadProgress(int)), SLOT(setProgress(int))); - connect(view, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool))); + connect(view, &QWebEngineView::loadFinished, this, &MainWindow::adjustLocation); + connect(view, &QWebEngineView::titleChanged, this, &MainWindow::adjustTitle); + connect(view, &QWebEngineView::loadProgress, this, &MainWindow::setProgress); + connect(view, &QWebEngineView::loadFinished, this, &MainWindow::finishLoading); locationEdit = new QLineEdit(this); locationEdit->setSizePolicy(QSizePolicy::Expanding, locationEdit->sizePolicy().verticalPolicy()); - connect(locationEdit, SIGNAL(returnPressed()), SLOT(changeLocation())); + connect(locationEdit, &QLineEdit::returnPressed, this, &MainWindow::changeLocation); QToolBar *toolBar = addToolBar(tr("Navigation")); toolBar->addAction(view->pageAction(QWebEnginePage::Back)); @@ -99,38 +83,40 @@ MainWindow::MainWindow(const QUrl& url) toolBar->addWidget(locationEdit); QMenu *viewMenu = menuBar()->addMenu(tr("&View")); - QAction* viewSourceAction = new QAction("Page Source", this); - connect(viewSourceAction, SIGNAL(triggered()), SLOT(viewSource())); + QAction *viewSourceAction = new QAction(tr("Page Source"), this); + connect(viewSourceAction, &QAction::triggered, this, &MainWindow::viewSource); viewMenu->addAction(viewSourceAction); QMenu *effectMenu = menuBar()->addMenu(tr("&Effect")); - effectMenu->addAction("Highlight all links", this, SLOT(highlightAllLinks())); + effectMenu->addAction(tr("Highlight all links"), this, &MainWindow::highlightAllLinks); rotateAction = new QAction(this); rotateAction->setIcon(style()->standardIcon(QStyle::SP_FileDialogDetailedView)); rotateAction->setCheckable(true); rotateAction->setText(tr("Turn images upside down")); - connect(rotateAction, SIGNAL(toggled(bool)), this, SLOT(rotateImages(bool))); + connect(rotateAction, &QAction::toggled, this, &MainWindow::rotateImages); effectMenu->addAction(rotateAction); QMenu *toolsMenu = menuBar()->addMenu(tr("&Tools")); - toolsMenu->addAction(tr("Remove GIF images"), this, SLOT(removeGifImages())); - toolsMenu->addAction(tr("Remove all inline frames"), this, SLOT(removeInlineFrames())); - toolsMenu->addAction(tr("Remove all object elements"), this, SLOT(removeObjectElements())); - toolsMenu->addAction(tr("Remove all embedded elements"), this, SLOT(removeEmbeddedElements())); + toolsMenu->addAction(tr("Remove GIF images"), this, &MainWindow::removeGifImages); + toolsMenu->addAction(tr("Remove all inline frames"), this, &MainWindow::removeInlineFrames); + toolsMenu->addAction(tr("Remove all object elements"), this, &MainWindow::removeObjectElements); + toolsMenu->addAction(tr("Remove all embedded elements"), this, &MainWindow::removeEmbeddedElements); setCentralWidget(view); } void MainWindow::viewSource() { - QTextEdit* textEdit = new QTextEdit(NULL); + QTextEdit *textEdit = new QTextEdit(nullptr); textEdit->setAttribute(Qt::WA_DeleteOnClose); textEdit->adjustSize(); textEdit->move(this->geometry().center() - textEdit->rect().center()); textEdit->show(); - view->page()->toHtml(invoke(textEdit, &QTextEdit::setPlainText)); + view->page()->toHtml([textEdit](const QString &html){ + textEdit->setPlainText(html); + }); } void MainWindow::adjustLocation() @@ -150,7 +136,7 @@ void MainWindow::adjustTitle() if (progress <= 0 || progress >= 100) setWindowTitle(view->title()); else - setWindowTitle(QString("%1 (%2%)").arg(view->title()).arg(progress)); + setWindowTitle(QStringLiteral("%1 (%2%)").arg(view->title()).arg(progress)); } void MainWindow::setProgress(int p) @@ -170,7 +156,7 @@ void MainWindow::finishLoading(bool) void MainWindow::highlightAllLinks() { - QString code = "qt.jQuery('a').each( function () { qt.jQuery(this).css('background-color', 'yellow') } ); undefined"; + QString code = QStringLiteral("qt.jQuery('a').each( function () { qt.jQuery(this).css('background-color', 'yellow') } )"); view->page()->runJavaScript(code); } @@ -179,32 +165,32 @@ void MainWindow::rotateImages(bool invert) QString code; if (invert) - code = "qt.jQuery('img').each( function () { qt.jQuery(this).css('-webkit-transition', '-webkit-transform 2s'); qt.jQuery(this).css('-webkit-transform', 'rotate(180deg)') } ); undefined"; + code = QStringLiteral("qt.jQuery('img').each( function () { qt.jQuery(this).css('transition', 'transform 2s'); qt.jQuery(this).css('transform', 'rotate(180deg)') } )"); else - code = "qt.jQuery('img').each( function () { qt.jQuery(this).css('-webkit-transition', '-webkit-transform 2s'); qt.jQuery(this).css('-webkit-transform', 'rotate(0deg)') } ); undefined"; + code = QStringLiteral("qt.jQuery('img').each( function () { qt.jQuery(this).css('transition', 'transform 2s'); qt.jQuery(this).css('transform', 'rotate(0deg)') } )"); view->page()->runJavaScript(code); } void MainWindow::removeGifImages() { - QString code = "qt.jQuery('[src*=gif]').remove()"; + QString code = QStringLiteral("qt.jQuery('[src*=gif]').remove()"); view->page()->runJavaScript(code); } void MainWindow::removeInlineFrames() { - QString code = "qt.jQuery('iframe').remove()"; + QString code = QStringLiteral("qt.jQuery('iframe').remove()"); view->page()->runJavaScript(code); } void MainWindow::removeObjectElements() { - QString code = "qt.jQuery('object').remove()"; + QString code = QStringLiteral("qt.jQuery('object').remove()"); view->page()->runJavaScript(code); } void MainWindow::removeEmbeddedElements() { - QString code = "qt.jQuery('embed').remove()"; + QString code = QStringLiteral("qt.jQuery('embed').remove()"); view->page()->runJavaScript(code); } -- cgit v1.2.3