Open Web Page in QWebView: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Replaced ''' with * in the code)
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
[[Category:Developing_with_Qt::General]]<br />[[Category:Developing_with_Qt::QtWebKit]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]
{{LangSwitch}}
[[Category:Developing with Qt::QtWebKit]]
[[Category:HowTo]]
[[Category:Snippets::Misc]]
[[Category:Tutorial]]
The following tutorial shows how to load a web page using {{DocLink|QUrl}} in {{DocLink|QWebView}}. QWebView is a widget provided by [http://doc.qt.io/qt-5/qtwebkit-index.html WebKit in Qt] that is used to view and edit web documents.


[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
* Specify that you want to link against the QtWebkit module by adding this line to your qmake .pro file:
 
<code>
QT += webkit
</code>
 
* Include required headers
 
<code>
#include <QWebView>
#include <QUrl>
</code>
 
* Create instance of QWebView
 
<code>
m_pWebView = new QWebView(this);
//set position and size
m_pWebView->setGeometry(0,0,200,200);
</code>
 
Additionally QWebView style can be customized using '''setStyleSheet()'''.
 
* Load a web page
 
<code>
m_pWebView->load(QUrl("http://www.example.com"));
</code>
 
== Example ==
This example has been built with Qt SDK 1.1 and tested on Symbian^3 devices.
 
=== mainwindow.h ===
<code>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QWebView>
#include <QUrl>
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
 
explicit MainWindow(QWidget *parent = 0);
virtual ~MainWindow();
 
private:
 
QWebView* m_pWebView;
};
 
#endif // MAINWINDOW_H
</code>
 
=== mainwindow.cpp ===
<code>
#include "mainwindow.h"
 
#include <QCoreApplication>
 
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
m_pWebView = new QWebView(this);
//set position and size
m_pWebView->setGeometry(0,0,200,200);
m_pWebView->load(QUrl("http://www.example.com"));
}
 
MainWindow::~MainWindow()
{
}
</code>
 
=== main.cpp ===
<code>
#include "mainwindow.h"
 
#include <QApplication>


'''English''' [[Open_Web_Page_in_QWebView_Bulgarian|Български]]
int main(int argc, char *argv[])
{
QApplication app(argc, argv);


= Open Web Page in QWebView =
MainWindow mainWindow;
mainWindow.showMaximized();
return app.exec();
}


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


* Specify that you want to link against the QtWebkit module by adding this line to your qmake .pro file:
== Troubleshooting ==
''QWebView: No such file or directory''
 
Make sure you have added '''webkit''' to the .pro file of the project.
 
<code>
QT+= webkit
</code>
 
If ''QWebView'' is still not resolved, try adding '''webkitwidgets''' as well.


<code><br />QT ''= webkit<br /></code>
<code>
<br />* Include required headers
QT+= webkit webkitwidgets
<br /><code><br />#include &lt;QWebView&amp;gt;<br />#include &lt;QUrl&amp;gt;<br /></code>
</code>
<br />* Create instance of QWebView
<br /><code><br />m_pWebView = new QWebView(this);<br />//set position and size<br />m_pWebView-&gt;setGeometry(0,0,200,200);<br /></code>
<br />Additionally QWebView style can be customized using '''setStyleSheet()'''.
<br />* Load a web page
<br /><code><br />m_pWebView-&gt;load(QUrl(&quot;http://www.example.com&amp;quot;));<br /></code>
<br />h2. Example
<br />This example has been built with Qt SDK 1.1 and tested on Symbian^3 devices.
<br />h3. mainwindow.h
<br /><code><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 /></code>
<br />h3. mainwindow.cpp
<br /><code><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 /></code>
<br />h3. main.cpp
<br /><code><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 /></code>
<br />h2. Troubleshooting
<br />''' '''QWebView: No such file or directory'''
<br />Make sure you have added '''webkit''' to the .pro file of the project.
<br /><code><br />QT''= webkit<br /></code>


= See also =
= See also =


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

Latest revision as of 19:48, 29 December 2017

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

The following tutorial shows how to load a web page using QUrl in QWebView. QWebView is a widget provided by WebKit in Qt 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:
QT += webkit
  • Include required headers
#include <QWebView>
#include <QUrl>
  • Create instance of QWebView
m_pWebView = new QWebView(this);
//set position and size
m_pWebView->setGeometry(0,0,200,200);

Additionally QWebView style can be customized using setStyleSheet().

  • Load a web page
m_pWebView->load(QUrl("http://www.example.com"));

Example

This example has been built with Qt SDK 1.1 and tested on Symbian^3 devices.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWebView>
#include <QUrl>

namespace Ui {
 class MainWindow;
}

class MainWindow : public QMainWindow
{
 Q_OBJECT
public:

 explicit MainWindow(QWidget *parent = 0);
 virtual ~MainWindow();

private:

 QWebView* m_pWebView;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

#include <QCoreApplication>

MainWindow::MainWindow(QWidget *parent)
 : QMainWindow(parent)
{
 m_pWebView = new QWebView(this);
 //set position and size
 m_pWebView->setGeometry(0,0,200,200);
 m_pWebView->load(QUrl("http://www.example.com"));
}

MainWindow::~MainWindow()
{
}

main.cpp

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
 QApplication app(argc, argv);

 MainWindow mainWindow;
 mainWindow.showMaximized();
 return app.exec();
}

Troubleshooting

QWebView: No such file or directory

Make sure you have added webkit to the .pro file of the project.

QT+= webkit

If QWebView is still not resolved, try adding webkitwidgets as well.

QT+= webkit webkitwidgets

See also

Embed YouTube Video in QWebView