Open Web Page in QWebView
[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 <QWebView&gt;<br />#include <QUrl&gt;<br />
* Create instance of QWebView
<br />m_pWebView = new QWebView(this);<br />//set position and size<br />m_pWebView->setGeometry(0,0,200,200);<br />
Additionally QWebView style can be customized using setStyleSheet().
* Load a web page
<br />m_pWebView->load(QUrl("http://www.example.com&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 <QtGui/QMainWindow&gt;<br />#include <QWebView&gt;<br />#include <QUrl&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 "mainwindow.h&quot;
<br />#include <QtCore/QCoreApplication&gt;
<br />MainWindow::MainWindow(QWidget *parent)<br /> : QMainWindow(parent)<br />{<br /> m_pWebView = new QWebView(this);<br /> //set position and size<br /> m_pWebView->setGeometry(0,0,200,200);<br /> m_pWebView->load(QUrl("http://www.example.com&quot;));<br />}
<br />MainWindow::~MainWindow()<br />{
<br />}<br />
h3. main.cpp
<br />#include "mainwindow.h&quot;
<br />#include <QtGui/QApplication&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;#40;&#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":http://developer.qt.nokia.com/wiki/Embed_YouTube_Video_in_QWebView