---
title: "qt开发webkit"
author: "qvb3d"
date: "2012-05-04T13:24:12Z"
tags: []
categories: ["编程语言"]
slug: "qt-webkit"
draft: false
lastmod: "2012-05-04T13:34:39Z"
id: 1511247
url: "https://qvb3d.iteye.com/blog/1511247"
---

昨天遇到一个兄台,说要qt写界面

有时界面会变动很大

我的建议是用　qtwebkit

具体是什么样的呢,也不知道那个兄台能不能看到这博客

把以前项目中的内容,简化成例子

 

为这个项目做的模拟器

![](http://dl.iteye.com/upload/attachment/0067/5488/74466962-243e-361b-9f64-15081ef1e76a.png)

发上来,朋友们看看

 

mkdir qwebkit

![](http://dl.iteye.com/upload/attachment/0067/5465/281d94b1-bcec-3a05-aba7-085e45000490.png)

 

cd qwebkit

 

QtDesigner 

![](http://dl.iteye.com/upload/attachment/0067/5461/f4047622-7308-3d17-9ea3-fc000e9d6d0e.png)

 建立form.ui 到qwebkit

![](http://dl.iteye.com/upload/attachment/0067/5481/d537a7dd-03ee-3be8-8477-7168b8e9f7ff.png)

![](http://dl.iteye.com/upload/attachment/0067/5467/8d236b56-8fed-3da8-9a60-7941d20f2da4.png)

 控件名   webView  

![](http://dl.iteye.com/upload/attachment/0067/5471/04a09b0a-d03e-3825-9d08-d41731b32153.png)

 

控件名 lineEdit

拖动调整大小

![](http://dl.iteye.com/upload/attachment/0067/5479/c7391e5f-f8d4-382d-bcc3-de01024575f5.png)  
 

![](http://dl.iteye.com/upload/attachment/0067/5473/3b3f547c-f31b-37ba-9815-b2611a5c3172.png)

 

保存

 

Qt Creator 建立qwebkit.qrc

 

也可以

vi qwebkit.qrc 

 

 

```
<!DOCTYPE RCC><RCC version="1.0">
 <qresource>
     <file>test.htm</file>
 </qresource>
</RCC> 
```

 

 

vi test.htm

 

```
<html>
<style type="text/css">
.boxshadow1 {
	-moz-box-shadow: 0 2px 10px 1px rgba(255, 0, 0, 0.2);
	-webkit-box-shadow: 0 2px 10px 1px rgba(255, 0, 0, 0.2);
	box-shadow: 0 2px 10px 1px rgba(255, 0, 0, 0.2);
	background: -webkit-gradient(linear, 0 20, 0 100%, from(#EEF), to(#BBB));
	border-radius: 15px 5px 25px 0px;
	position:absolute; 
	top:10px; left:50px; 
	height:76px; 
	width:150px; 
	font-size:30px;
	border-top-width: 1px;
	border-right-width: 1px;
	border-bottom-width: 1px;
	border-left-width: 1px;
	border-top-style: solid;
	border-right-style: solid;
	border-bottom-style: solid;
	border-left-style: solid;
	border-top-color: #EEE;
	border-right-color: #333;
	border-bottom-color: #333;
	border-left-color: #EEE;
}
</style>
<body>
<div width="100px" height="50px" class="boxshadow1" >
<span>html 5</span></div>
<br><br><br><br><br>
<form onsubmit="qtwebkit.submit()">

            <input type="text" id="username">
            <input type="submit" value="Submit">
</form>
</body>
</html>
```

 

vi qtwebkit.h

 

 

```
#ifndef QWEBKIT_H
#define QWEBKIT_H

#include <QtGui/QWidget>
#include <QWebFrame>
#include <QWebElement>
#include <QMainWindow>
#include <QtGui>
#include "ui_form.h"

class webkit_test : public QWidget
{
    Q_OBJECT

public:
    webkit_test(QWidget *parent = 0, Qt::WFlags flags = 0)
    {
    ui.setupUi(this);
    ui.webView->setUrl(QUrl("qrc:/test.htm"));
    connect(ui.webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
            this, SLOT(JavaScriptWindowObject()));
    }
    ~webkit_test()
    {
    exit(0);
    }

public slots:
    void submit()
    {
    QWebFrame *frame = ui.webView->page()->mainFrame();

    QWebElement firstName = frame->findFirstElement("#username");

    ui.lineEdit->setText(firstName.evaluateJavaScript("this.value").toString());

    }
    void JavaScriptWindowObject()
    {
     ui.webView->page()->mainFrame()->addToJavaScriptWindowObject("qtwebkit", this);
    }
private:
    Ui::Form ui;
};









class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow()
    {
    webkitWidget = new webkit_test(this);
    setCentralWidget(webkitWidget);
    }

private:
    webkit_test *webkitWidget;

};


#endif
```

 

vi main.cpp

 

 

```
#include <QtGui/QApplication>
#include "qtwebkit.h"

int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(qwebkit);
    QApplication app(argc, argv);
    MainWindow mainWindow;
    mainWindow.setWindowTitle("Test qtwebkit");
    mainWindow.resize(800,600);
    mainWindow.show();
    return app.exec();
}
```

 

 

export PATH=/usr/local/qt4.6/bin:$PATH

qmake --version

     QMake version 2.01a

     Using Qt version 4.6.3 in /usr/local/qt4.6//lib

 

qmake -project

vi qwebkit.pro 

在最开始加入

QT          +=  webkit network

 

改 TARGET =qtwebkit

 

再执行

qmake

make

运行结果

![](http://dl.iteye.com/upload/attachment/0067/5486/49e121e9-1e8f-3d1f-ae0b-33464707fd68.png)