Qt 5.10 Book Examples
WebBrowser.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // Calculator.h -
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #include <QtGui>
12 #include <QtWebEngineWidgets>
13 #include "WebBrowser.h"
14 
15 // ----------------------------------------------------------------------
16 WebBrowser::WebBrowser(QWidget* wgt/*=0*/) : QWidget(wgt)
17 {
18  m_ptxt = new QLineEdit("http://www.bhv.ru");
19  m_pwv = new QWebEngineView;
20  m_pcmdBack = new QPushButton("<");
21  m_pcmdForward = new QPushButton(">");
22 
23  m_pcmdBack->setEnabled(false);
24  m_pcmdForward->setEnabled(false);
25 
26  QProgressBar* pprb = new QProgressBar;
27  QPushButton* pcmdGo = new QPushButton("&Go");
28  QPushButton* pcmdStop = new QPushButton("&Stop");
29  QPushButton* pcmdRefresh = new QPushButton("&Refresh");
30 
31  connect(pcmdGo, SIGNAL(clicked()), SLOT(slotGo()));
32  connect(m_ptxt, SIGNAL(returnPressed()), SLOT(slotGo()));
33  connect(m_pcmdBack, SIGNAL(clicked()), m_pwv, SLOT(back()));
34  connect(m_pcmdForward, SIGNAL(clicked()), m_pwv, SLOT(forward()));
35  connect(pcmdRefresh, SIGNAL(clicked()), m_pwv, SLOT(reload()));
36  connect(pcmdStop, SIGNAL(clicked()), m_pwv, SLOT(stop()));
37  connect(m_pwv, SIGNAL(loadProgress(int)), pprb, SLOT(setValue(int)));
38  connect(m_pwv, SIGNAL(loadFinished(bool)), SLOT(slotFinished(bool)));
39 
40  //Layout setup
41  QHBoxLayout* phbx = new QHBoxLayout;
42  phbx->addWidget(m_pcmdBack);
43  phbx->addWidget(m_pcmdForward);
44  phbx->addWidget(pcmdStop);
45  phbx->addWidget(pcmdRefresh);
46  phbx->addWidget(m_ptxt);
47  phbx->addWidget(pcmdGo);
48 
49  QVBoxLayout* playout = new QVBoxLayout;
50  playout->addLayout(phbx);
51  playout->addWidget(m_pwv);
52  playout->addWidget(pprb);
53  setLayout(playout);
54 
55  slotGo();
56 }
57 
58 // ----------------------------------------------------------------------
59 void WebBrowser::slotGo()
60 {
61  if (!m_ptxt->text().startsWith("ftp://")
62  && !m_ptxt->text().startsWith("http://")
63  && !m_ptxt->text().startsWith("gopher://")
64  ) {
65  m_ptxt->setText("http://" + m_ptxt->text());
66  }
67  m_pwv->load(QUrl(m_ptxt->text()));
68 }
69 
70 // ----------------------------------------------------------------------
71 void WebBrowser::slotFinished(bool bOk)
72 {
73  if (!bOk) {
74  m_pwv->setHtml("<CENTER>An error has occured"
75  " while loading the web page</CENTER>"
76  );
77  }
78 
79  m_ptxt->setText(m_pwv->url().toString());
80 
81  m_pcmdBack->setEnabled(m_pwv->page()->history()->canGoBack());
82  m_pcmdForward->setEnabled(m_pwv->page()->history()->canGoForward());
83 }
84 
WebBrowser(QWidget *wgt=0)
Definition: WebBrowser.cpp:16