<HTML><Scriptlanguage= "JavaScript"> functionQtcalljs () {alert ("SDFSD"); } functionjscallqt () {mainwindow.dosome (); }</Script><Body><Buttononclick= "JSCALLQT ()"style= = "height:20px;width:100px"/></Body></HTML>
Qt.h:
#ifndef Qttt_h#defineQttt_h#include<QtGui/QMainWindow>#include<QWebKitPlatformPlugin>#include<QWebFrame>#include<QMessageBox>#include<QObject>#include"ui_qttt.h"classQTTT: Publicqmainwindow{Q_object Public: qttt (Qwidget*parent =0, Qt::wflags flags =0); ~qttt ();Private: Ui::qtttclass Ui;PrivateSlots:voidpop (); voidAddmyobjecttojavascript (); Public: Q_invokablevoiddosome ();};#endif //Qttt_h
Qt.cpp
#include"qttt.h"qttt::qttt (Qwidget*parent, Qt::wflags flags): Qmainwindow (parent, flags) {UI.SETUPUI ( This); Ui.webview->load (Qurl ("test.html")); Connect (ui.pushbutton,signal (clicked)), This, SLOT (pop ())); Connect (Ui.webview->page ()->mainframe (), SIGNAL (javascriptwindowobjectcleared ()), This, SLOT (Addmyobjecttojavascript ()));} QTTT::~qttt () {}voidqttt::p op () {Ui.webview->page ()->mainframe ()->evaluatejavascript ("Qtcalljs ();");}voidQttt::addmyobjecttojavascript () {Ui.webview->page ()->mainframe ()->addtojavascriptwindowobject ("MainWindow", This);}voidqttt::d osome () {QMESSAGEBOX::ABOUTQT ( This,"SDFSA");}
QT Call Js,js call QT