这里主要讲一下QtWebKit的小应用,在Qt中建立一个Qt Application程序,我的程序的名字为QtWebKit。然后修改HelloWebKit.pro代码:
#-------------------------------------------------
#
# Project created by QtCreator 2014-10-21T18:23:11
#
#-------------------------------------------------
QT += core gui
QT += webkit
QT += webkitwidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = HelloWebKit
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
从Qt 4.4开始,已经在Qt中集成了WebKit的开发环境。修改HelloWebKit.pro的代码主要是为了在项目工程中加入webkit和webkitwidgets模块,由于我使用的是Qt5.3,在这个版本中QWebView属于QtWebKitWidgets而不是QtWebKit。所以要在pro中添加“QT += webkitwidgets”,使得该程序能够使用QWebView。
然后在main.cpp:
//main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtWebKit/QtWebKit>
#include <QWebView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWebView view;
view.load(QUrl("http://www.baidu.com"));
view.show();
return a.exec();
}
编译运行,得到下面的结果:
现在只能使用基本搜索,却不能使用跳转,主要是相应的QWebView的属性没有设置: