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)
 
(10 intermediate revisions by 6 users not shown)
Line 1: Line 1:
'''English''' [[Open Web Page in QWebView Bulgarian|Български]]
{{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.


=Open Web Page in QWebView=
* Specify that you want to link against the QtWebkit module by adding this line to your qmake .pro file:


The following tutorial shows how to load a web page using [http://doc.qt.nokia.com/latest/qurl.html QUrl] ''[doc.qt.nokia.com]'' in [http://doc.qt.nokia.com/latest/qwebview.html QWebView] ''[doc.qt.nokia.com]'' . QWebView is a widget provided by [http://doc.qt.nokia.com/latest/qtwebkit.html WebKit in Qt] ''[doc.qt.nokia.com]'' that is used to view and edit web documents.
<code>
QT += webkit
</code>


* Specify that you want to link against the QtWebkit module by adding this line to your qmake .pro file:
* Include required headers


* Include required headers
<code>
#include <QWebView>
#include <QUrl>
</code>


* Create instance of QWebView
* 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()'''.
Additionally QWebView style can be customized using '''setStyleSheet()'''.
Line 15: Line 31:
* Load a web page
* Load a web page


==Example==
<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.


This example has been built with Qt <span class="caps">SDK</span> 1.1 and tested on Symbian^3 devices.
=== mainwindow.h ===
<code>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H


===mainwindow.h===
#include <QMainWindow>
#include <QWebView>
#include <QUrl>


===mainwindow.cpp===
namespace Ui {
class MainWindow;
}


===main.cpp===
class MainWindow : public QMainWindow
{
Q_OBJECT
public:


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


* '''QWebView: No such file or directory'''
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>
 
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
 
MainWindow mainWindow;
mainWindow.showMaximized();
return app.exec();
}
 
</code>
 
== Troubleshooting ==
''QWebView: No such file or directory''


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


=See also=
<code>
 
QT+= webkit
[http://developer.qt.nokia.com/wiki/Embed_YouTube_Video_in_QWebView Embed YouTube Video in QWebView] ''[developer.qt.nokia.com]''<br />[http://developer.qt.nokia.com/wiki/Extending_QtWebKit Extending QtWebKit] ''[developer.qt.nokia.com]''
</code>


===Categories:===
If ''QWebView'' is still not resolved, try adding '''webkitwidgets''' as well.


* [[:Category:Developing with Qt|Developing_with_Qt]]
<code>
** [[:Category:Developing with Qt::General|General]]
QT+= webkit webkitwidgets
* [[:Category:Developing with Qt::QtWebKit|QtWebKit]]
</code>


* [[:Category:Developing with Qt::Qt Quick::Tutorial|Tutorial]]
= See also =


* [[:Category:HowTo|HowTo]]
[[Embed YouTube Video in QWebView]]
* [[:Category:snippets|snippets]]

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