Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Open Web Page in QWebView: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Formatting)
(Replaced ''' with * in the code)
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
[[Category:Developing with Qt::QtWebKit]]
[[Category:Developing_with_Qt::General]]
[[Category:Developing_with_Qt::QtWebKit]]
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Snippets::Misc]]
[[Category:Tutorial]]
[[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.
 
'''English''' [[Open_Web_Page_in_QWebView_Bulgarian|Български]]
 
 
 
The following tutorial shows how to load a web page using [http://doc.qt.io/qt-5/qurl.html QUrl] in [http://doc.qt.io/qt-5/qwebview.html 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.


* Specify that you want to link against the QtWebkit module by adding this line to your qmake .pro file:
* Specify that you want to link against the QtWebkit module by adding this line to your qmake .pro file:
Line 51: Line 43:
#define MAINWINDOW_H
#define MAINWINDOW_H


#include <QtGui/QMainWindow>
#include <QMainWindow>
#include <QWebView>
#include <QWebView>
#include <QUrl>
#include <QUrl>
Line 64: Line 56:
public:
public:


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


private:
private:


  QWebView''' m_pWebView;
  QWebView* m_pWebView;
};
};


Line 79: Line 71:
#include "mainwindow.h"
#include "mainwindow.h"


#include <QtCore/QCoreApplication>
#include <QCoreApplication>


MainWindow::MainWindow(QWidget *parent)
MainWindow::MainWindow(QWidget *parent)
Line 92: Line 84:
MainWindow::~MainWindow()
MainWindow::~MainWindow()
{
{
}
}
</code>
</code>
Line 100: Line 91:
#include "mainwindow.h"
#include "mainwindow.h"


#include <QtGui/QApplication>
#include <QApplication>


int main(int argc, char '''argv[])
int main(int argc, char *argv[])
{
{
  QApplication app(argc, argv);
  QApplication app(argc, argv);
Line 114: Line 105:


== Troubleshooting ==
== Troubleshooting ==
''' '''QWebView: No such file or directory'''
''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.
Line 120: Line 111:
<code>
<code>
QT+= webkit
QT+= webkit
</code>
If ''QWebView'' is still not resolved, try adding '''webkitwidgets''' as well.
<code>
QT+= webkit webkitwidgets
</code>
</code>


= See also =
= See also =


[http://developer.qt.nokia.com/wiki/Embed_YouTube_Video_in_QWebView 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