QtWebKit初接触

     这里主要讲一下QtWebKit的小应用,在Qt中建立一个Qt Application程序,我的程序的名字为QtWebKit。然后修改HelloWebKit.pro代码:

[cpp] view plain copy print ?
  1. #-------------------------------------------------  
  2. #  
  3. # Project created by QtCreator 2014-10-21T18:23:11  
  4. #  
  5. #-------------------------------------------------  
  6. QT       += core gui  
  7. QT       += webkit  
  8. QT       += webkitwidgets  
  9. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets  
  10. TARGET = HelloWebKit  
  11. TEMPLATE = app  
  12. SOURCES += main.cpp\  
  13.         mainwindow.cpp  
  14. HEADERS  += mainwindow.h  
  15. FORMS    += mainwindow.ui  

     从Qt 4.4开始,已经在Qt中集成了WebKit的开发环境。修改HelloWebKit.pro的代码主要是为了在项目工程中加入webkit和webkitwidgets模块,由于我使用的是Qt5.3,在这个版本中QWebView属于QtWebKitWidgets而不是QtWebKit。所以要在pro中添加“QT += webkitwidgets”,使得该程序能够使用QWebView。
然后在main.cpp:

[cpp] view plain copy print ?
  1. //main.cpp  
  2. #include "mainwindow.h"  
  3. #include <QApplication>  
  4. #include <QtWebKit/QtWebKit>  
  5. #include <QWebView>  
  6. int main(int argc, char *argv[])  
  7. {  
  8.     QApplication a(argc, argv);  
  9.     QWebView view;  
  10.     view.load(QUrl("http://www.baidu.com"));  
  11.     view.show();  
  12.     return a.exec();  
  13. }  

编译运行,得到下面的结果:


现在只能使用基本搜索,却不能使用跳转,主要是相应的QWebView的属性没有设置:

QtWebKit初接触_第1张图片


你可能感兴趣的:(QtWebKit初接触)