WebEngine Widgets Simple Browser 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
"tabwidget.h"
#include
"webpage.h"
#include
"webview.h"
#include <QLabel>
#include <QMenu>
#include <QTabBar>
#include <QWebEngineProfile>
TabWidget::
TabWidget(QWebEngineProfile *
profile, QWidget *
parent)
:
QTabWidget(parent)
, m_profile(profile)
{
QTabBar *
tabBar =
this
-&
gt;tabBar();
tabBar-&
gt;setTabsClosable(true
);
tabBar-&
gt;setSelectionBehaviorOnRemove(QTabBar::
SelectPreviousTab);
tabBar-&
gt;setMovable(true
);
tabBar-&
gt;setContextMenuPolicy(Qt::
CustomContextMenu);
connect(tabBar, &
amp;QTabBar::
customContextMenuRequested, this
, &
amp;TabWidget::
handleContextMenuRequested);
connect(tabBar, &
amp;QTabBar::
tabCloseRequested, this
, &
amp;TabWidget::
closeTab);
connect(tabBar, &
amp;QTabBar::
tabBarDoubleClicked, [this
](int
index) {
if
(index ==
-
1
)
createTab();
}
);
setDocumentMode(true
);
setElideMode(Qt::
ElideRight);
connect(this
, &
amp;QTabWidget::
currentChanged, this
, &
amp;TabWidget::
handleCurrentChanged);
if
(profile-&
gt;isOffTheRecord()) {
QLabel *
icon =
new
QLabel(this
);
QPixmap pixmap(QStringLiteral(":ninja.png"
));
icon-&
gt;setPixmap(pixmap.scaledToHeight(tabBar-&
gt;height()));
setStyleSheet(QStringLiteral("QTabWidget::tab-bar { left: %1px; }"
).
arg(icon-&
gt;pixmap()-&
gt;width()));
}
}
void
TabWidget::
handleCurrentChanged(int
index)
{
if
(index !=
-
1
) {
WebView *
view =
webView(index);
if
(!
view-&
gt;url().isEmpty())
view-&
gt;setFocus();
emit titleChanged(view-&
gt;title());
emit loadProgress(view-&
gt;loadProgress());
emit urlChanged(view-&
gt;url());
emit favIconChanged(view-&
gt;favIcon());
emit webActionEnabledChanged(QWebEnginePage::
Back, view-&
gt;isWebActionEnabled(QWebEnginePage::
Back));
emit webActionEnabledChanged(QWebEnginePage::
Forward, view-&
gt;isWebActionEnabled(QWebEnginePage::
Forward));
emit webActionEnabledChanged(QWebEnginePage::
Stop, view-&
gt;isWebActionEnabled(QWebEnginePage::
Stop));
emit webActionEnabledChanged(QWebEnginePage::
Reload,view-&
gt;isWebActionEnabled(QWebEnginePage::
Reload));
}
else
{
emit titleChanged(QString());
emit loadProgress(0
);
emit urlChanged(QUrl());
emit favIconChanged(QIcon());
emit webActionEnabledChanged(QWebEnginePage::
Back, false
);
emit webActionEnabledChanged(QWebEnginePage::
Forward, false
);
emit webActionEnabledChanged(QWebEnginePage::
Stop, false
);
emit webActionEnabledChanged(QWebEnginePage::
Reload, true
);
}
}
void
TabWidget::
handleContextMenuRequested(const
QPoint &
amp;pos)
{
QMenu menu;
menu.addAction(tr("New &Tab"
), this
, &
amp;TabWidget::
createTab, QKeySequence::
AddTab);
int
index =
tabBar()-&
gt;tabAt(pos);
if
(index !=
-
1
) {
QAction *
action =
menu.addAction(tr("Clone Tab"
));
connect(action, &
amp;QAction::
triggered, this
, [this
,index]() {
cloneTab(index);
}
);
menu.addSeparator();
action =
menu.addAction(tr("&Close Tab"
));
action-&
gt;setShortcut(QKeySequence::
Close);
connect(action, &
amp;QAction::
triggered, this
, [this
,index]() {
closeTab(index);
}
);
action =
menu.addAction(tr("Close &Other Tabs"
));
connect(action, &
amp;QAction::
triggered, this
, [this
,index]() {
closeOtherTabs(index);
}
);
menu.addSeparator();
action =
menu.addAction(tr("Reload Tab"
));
action-&
gt;setShortcut(QKeySequence::
Refresh);
connect(action, &
amp;QAction::
triggered, this
, [this
,index]() {
reloadTab(index);
}
);
}
else
{
menu.addSeparator();
}
menu.addAction(tr("Reload All Tabs"
), this
, &
amp;TabWidget::
reloadAllTabs);
menu.exec(QCursor::
pos());
}
WebView *
TabWidget::
currentWebView() const
{
return
webView(currentIndex());
}
WebView *
TabWidget::
webView(int
index) const
{
return
qobject_cast&
lt;WebView*&
gt;(widget(index));
}
void
TabWidget::
setupView(WebView *
webView)
{
QWebEnginePage *
webPage =
webView-&
gt;page();
connect(webView, &
amp;QWebEngineView::
titleChanged, [this
, webView](const
QString &
amp;title) {
int
index =
indexOf(webView);
if
(index !=
-
1
) {
setTabText(index, title);
setTabToolTip(index, title);
}
if
(currentIndex() ==
index)
emit titleChanged(title);
}
);
connect(webView, &
amp;QWebEngineView::
urlChanged, [this
, webView](const
QUrl &
amp;url) {
int
index =
indexOf(webView);
if
(index !=
-
1
)
tabBar()-&
gt;setTabData(index, url);
if
(currentIndex() ==
index)
emit urlChanged(url);
}
);
connect(webView, &
amp;QWebEngineView::
loadProgress, [this
, webView](int
progress) {
if
(currentIndex() ==
indexOf(webView))
emit loadProgress(progress);
}
);
connect(webPage, &
amp;QWebEnginePage::
linkHovered, [this
, webView](const
QString &
amp;url) {
if
(currentIndex() ==
indexOf(webView))
emit linkHovered(url);
}
);
connect(webView, &
amp;WebView::
favIconChanged, [this
, webView](const
QIcon &
amp;icon) {
int
index =
indexOf(webView);
if
(index !=
-
1
)
setTabIcon(index, icon);
if
(currentIndex() ==
index)
emit favIconChanged(icon);
}
);
connect(webView, &
amp;WebView::
webActionEnabledChanged, [this
, webView](QWebEnginePage::
WebAction action, bool
enabled) {
if
(currentIndex() ==
indexOf(webView))
emit webActionEnabledChanged(action,enabled);
}
);
connect(webPage, &
amp;QWebEnginePage::
windowCloseRequested, [this
, webView]() {
int
index =
indexOf(webView);
if
(index &
gt;=
0
)
closeTab(index);
}
);
connect(webView, &
amp;WebView::
devToolsRequested, this
, &
amp;TabWidget::
devToolsRequested);
}
WebView *
TabWidget::
createTab()
{
WebView *
webView =
createBackgroundTab();
setCurrentWidget(webView);
return
webView;
}
WebView *
TabWidget::
createBackgroundTab()
{
WebView *
webView =
new
WebView;
WebPage *
webPage =
new
WebPage(m_profile, webView);
webView-&
gt;setPage(webPage);
setupView(webView);
int
index =
addTab(webView, tr("(Untitled)"
));
setTabIcon(index, webView-&
gt;favIcon());
// Workaround for QTBUG-61770
webView-&
gt;resize(currentWidget()-&
gt;size());
webView-&
gt;show();
return
webView;
}
void
TabWidget::
reloadAllTabs()
{
for
(int
i =
0
; i &
lt; count(); ++
i)
webView(i)-&
gt;reload();
}
void
TabWidget::
closeOtherTabs(int
index)
{
for
(int
i =
count() -
1
; i &
gt; index; --
i)
closeTab(i);
for
(int
i =
index -
1
; i &
gt;=
0
; --
i)
closeTab(i);
}
void
TabWidget::
closeTab(int
index)
{
if
(WebView *
view =
webView(index)) {
bool
hasFocus =
view-&
gt;hasFocus();
removeTab(index);
if
(hasFocus &
amp;&
amp; count() &
gt; 0
)
currentWebView()-&
gt;setFocus();
if
(count() ==
0
)
createTab();
view-&
gt;deleteLater();
}
}
void
TabWidget::
cloneTab(int
index)
{
if
(WebView *
view =
webView(index)) {
WebView *
tab =
createTab();
tab-&
gt;setUrl(view-&
gt;url());
}
}
void
TabWidget::
setUrl(const
QUrl &
amp;url)
{
if
(WebView *
view =
currentWebView()) {
view-&
gt;setUrl(url);
view-&
gt;setFocus();
}
}
void
TabWidget::
triggerWebPageAction(QWebEnginePage::
WebAction action)
{
if
(WebView *
webView =
currentWebView()) {
webView-&
gt;triggerPageAction(action);
webView-&
gt;setFocus();
}
}
void
TabWidget::
nextTab()
{
int
next =
currentIndex() +
1
;
if
(next ==
count())
next =
0
;
setCurrentIndex(next);
}
void
TabWidget::
previousTab()
{
int
next =
currentIndex() -
1
;
if
(next &
lt; 0
)
next =
count() -
1
;
setCurrentIndex(next);
}
void
TabWidget::
reloadTab(int
index)
{
if
(WebView *
view =
webView(index))
view-&
gt;reload();
}