WebEngine Content Manipulation Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include <QtWidgets>
#include <QtWebEngineWidgets>
#include
"mainwindow.h"
MainWindow::
MainWindow(const
QUrl&
amp; url)
{
setAttribute(Qt::
WA_DeleteOnClose, true
);
progress =
0
;
QFile file;
file.setFileName(":/jquery.min.js"
);
file.open(QIODevice::
ReadOnly);
jQuery =
file.readAll();
jQuery.append("
\n
var qt = { 'jQuery': jQuery.noConflict(true) };"
);
file.close();
view =
new
QWebEngineView(this
);
view-&
gt;load(url);
connect(view, &
amp;QWebEngineView::
loadFinished, this
, &
amp;MainWindow::
adjustLocation);
connect(view, &
amp;QWebEngineView::
titleChanged, this
, &
amp;MainWindow::
adjustTitle);
connect(view, &
amp;QWebEngineView::
loadProgress, this
, &
amp;MainWindow::
setProgress);
connect(view, &
amp;QWebEngineView::
loadFinished, this
, &
amp;MainWindow::
finishLoading);
locationEdit =
new
QLineEdit(this
);
locationEdit-&
gt;setSizePolicy(QSizePolicy::
Expanding, locationEdit-&
gt;sizePolicy().verticalPolicy());
connect(locationEdit, &
amp;QLineEdit::
returnPressed, this
, &
amp;MainWindow::
changeLocation);
QToolBar *
toolBar =
addToolBar(tr("Navigation"
));
toolBar-&
gt;addAction(view-&
gt;pageAction(QWebEnginePage::
Back));
toolBar-&
gt;addAction(view-&
gt;pageAction(QWebEnginePage::
Forward));
toolBar-&
gt;addAction(view-&
gt;pageAction(QWebEnginePage::
Reload));
toolBar-&
gt;addAction(view-&
gt;pageAction(QWebEnginePage::
Stop));
toolBar-&
gt;addWidget(locationEdit);
QMenu *
viewMenu =
menuBar()-&
gt;addMenu(tr("&View"
));
QAction *
viewSourceAction =
new
QAction(tr("Page Source"
), this
);
connect(viewSourceAction, &
amp;QAction::
triggered, this
, &
amp;MainWindow::
viewSource);
viewMenu-&
gt;addAction(viewSourceAction);
QMenu *
effectMenu =
menuBar()-&
gt;addMenu(tr("&Effect"
));
effectMenu-&
gt;addAction(tr("Highlight all links"
), this
, &
amp;MainWindow::
highlightAllLinks);
rotateAction =
new
QAction(this
);
rotateAction-&
gt;setIcon(style()-&
gt;standardIcon(QStyle::
SP_FileDialogDetailedView));
rotateAction-&
gt;setCheckable(true
);
rotateAction-&
gt;setText(tr("Turn images upside down"
));
connect(rotateAction, &
amp;QAction::
toggled, this
, &
amp;MainWindow::
rotateImages);
effectMenu-&
gt;addAction(rotateAction);
QMenu *
toolsMenu =
menuBar()-&
gt;addMenu(tr("&Tools"
));
toolsMenu-&
gt;addAction(tr("Remove GIF images"
), this
, &
amp;MainWindow::
removeGifImages);
toolsMenu-&
gt;addAction(tr("Remove all inline frames"
), this
, &
amp;MainWindow::
removeInlineFrames);
toolsMenu-&
gt;addAction(tr("Remove all object elements"
), this
, &
amp;MainWindow::
removeObjectElements);
toolsMenu-&
gt;addAction(tr("Remove all embedded elements"
), this
, &
amp;MainWindow::
removeEmbeddedElements);
setCentralWidget(view);
}
void
MainWindow::
viewSource()
{
QTextEdit *
textEdit =
new
QTextEdit(nullptr
);
textEdit-&
gt;setAttribute(Qt::
WA_DeleteOnClose);
textEdit-&
gt;adjustSize();
textEdit-&
gt;move(this
-&
gt;geometry().center() -
textEdit-&
gt;rect().center());
textEdit-&
gt;show();
view-&
gt;page()-&
gt;toHtml([textEdit](const
QString &
amp;html){
textEdit-&
gt;setPlainText(html);
}
);
}
void
MainWindow::
adjustLocation()
{
locationEdit-&
gt;setText(view-&
gt;url().toString());
}
void
MainWindow::
changeLocation()
{
QUrl url =
QUrl::
fromUserInput(locationEdit-&
gt;text());
view-&
gt;load(url);
view-&
gt;setFocus();
}
void
MainWindow::
adjustTitle()
{
if
(progress &
lt;=
0
||
progress &
gt;=
100
)
setWindowTitle(view-&
gt;title());
else
setWindowTitle(QStringLiteral("%1 (%2%)"
).arg(view-&
gt;title()).arg(progress));
}
void
MainWindow::
setProgress(int
p)
{
progress =
p;
adjustTitle();
}
void
MainWindow::
finishLoading(bool
)
{
progress =
100
;
adjustTitle();
view-&
gt;page()-&
gt;runJavaScript(jQuery);
rotateImages(rotateAction-&
gt;isChecked());
}
void
MainWindow::
highlightAllLinks()
{
QString code =
QStringLiteral("qt.jQuery('a').each( function () { qt.jQuery(this).css('background-color', 'yellow') } )"
);
view-&
gt;page()-&
gt;runJavaScript(code);
}
void
MainWindow::
rotateImages(bool
invert)
{
QString code;
if
(invert)
code =
QStringLiteral("qt.jQuery('img').each( function () { qt.jQuery(this).css('transition', 'transform 2s'); qt.jQuery(this).css('transform', 'rotate(180deg)') } )"
);
else
code =
QStringLiteral("qt.jQuery('img').each( function () { qt.jQuery(this).css('transition', 'transform 2s'); qt.jQuery(this).css('transform', 'rotate(0deg)') } )"
);
view-&
gt;page()-&
gt;runJavaScript(code);
}
void
MainWindow::
removeGifImages()
{
QString code =
QStringLiteral("qt.jQuery('[src*=gif]').remove()"
);
view-&
gt;page()-&
gt;runJavaScript(code);
}
void
MainWindow::
removeInlineFrames()
{
QString code =
QStringLiteral("qt.jQuery('iframe').remove()"
);
view-&
gt;page()-&
gt;runJavaScript(code);
}
void
MainWindow::
removeObjectElements()
{
QString code =
QStringLiteral("qt.jQuery('object').remove()"
);
view-&
gt;page()-&
gt;runJavaScript(code);
}
void
MainWindow::
removeEmbeddedElements()
{
QString code =
QStringLiteral("qt.jQuery('embed').remove()"
);
view-&
gt;page()-&
gt;runJavaScript(code);
}