Open Web Page in QWebView

From Qt Wiki
Revision as of 14:32, 23 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search





[toc align_right="yes" depth="3"]

English Български

Open Web Page in QWebView

The following tutorial shows how to load a web page using "QUrl":http://doc.qt.nokia.com/latest/qurl.html in "QWebView":http://doc.qt.nokia.com/latest/qwebview.html . QWebView is a widget provided by "WebKit in Qt":http://doc.qt.nokia.com/latest/qtwebkit.html that is used to view and edit web documents.

  • Specify that you want to link against the QtWebkit module by adding this line to your qmake .pro file:
<br />QT ''= webkit<br />


* Include required headers


<br />#include &lt;QWebView&amp;gt;<br />#include &lt;QUrl&amp;gt;<br />


* Create instance of QWebView


<br />m_pWebView = new QWebView(this);<br />//set position and size<br />m_pWebView-&gt;setGeometry(0,0,200,200);<br />


Additionally QWebView style can be customized using setStyleSheet().
* Load a web page


<br />m_pWebView-&gt;load(QUrl(&quot;http://www.example.com&amp;quot;));<br />


h2. Example
This example has been built with Qt SDK 1.1 and tested on Symbian^3 devices.
h3. mainwindow.h


<br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H
<br />#include &lt;QtGui/QMainWindow&amp;gt;<br />#include &lt;QWebView&amp;gt;<br />#include &lt;QUrl&amp;gt;
<br />namespace Ui {<br /> class MainWindow;<br />}
<br />class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT<br />public:
<br /> explicit MainWindow(QWidget '''parent = 0);<br /> virtual ~MainWindow();
<br />private:
<br /> QWebView''' m_pWebView;<br />};
<br />#endif // MAINWINDOW_H<br />


h3. mainwindow.cpp


<br />#include &quot;mainwindow.h&amp;quot;
<br />#include &lt;QtCore/QCoreApplication&amp;gt;
<br />MainWindow::MainWindow(QWidget *parent)<br /> : QMainWindow(parent)<br />{<br /> m_pWebView = new QWebView(this);<br /> //set position and size<br /> m_pWebView-&gt;setGeometry(0,0,200,200);<br /> m_pWebView-&gt;load(QUrl(&quot;http://www.example.com&amp;quot;));<br />}
<br />MainWindow::~MainWindow()<br />{
<br />}<br />


h3. main.cpp


<br />#include &quot;mainwindow.h&amp;quot;
<br />#include &lt;QtGui/QApplication&amp;gt;
<br />int main(int argc, char '''argv[])<br />{<br /> QApplication app(argc, argv);
<br /> MainWindow mainWindow;<br /> mainWindow.showMaximized();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}
<br />


h2. Troubleshooting
QWebView: No such file or directory
Make sure you have added webkit to the .pro file of the project.


<br />QT''= webkit<br />

See also

"Embed YouTube Video in QWebView&quot;:http://developer.qt.nokia.com/wiki/Embed_YouTube_Video_in_QWebView