Sometimes you may need to call the local object method on an HTML webpage.
For example, I click a link to a video file and want to call a local multimedia module to play the video file. How can this problem be solved?
I. using JavaScript in HTML to call the QT local object Method
The process is as follows:
- Expose local qobject objects to WebKit and JavaScript
- Use JavaScript to call the slot of the local qobject
Next, we will analyze each step in detail:
1. Expose the local qobject object to WebKit and JavaScript
Qwebframe provides interfaces to implement this function:
1 |
Void Addtojavascriptwindowobject ( Const Qstring & name, qobject * object, qscriptengine: valueownership ownership ); |
The help document of QT describes this method as follows:
Make object available under name from within the frame's JavaScript context. The object will be inserted as a child of the frame's window object.
Qt properties will be exposed as JavaScript properties and slots as Javascript methods.
If you want to ensure that your qobjects remain accessible after loading a new URL, you shoshould add them in a slot connected toJavascriptWindowobjectcleared () signal.
If Javascript is not enabled for this page, then this method does nothing.
The object will never be explicitly deleted by qtwebkit.
The addtojavascriptwindowobject method must be Javascript. To ensure that this method is called every time a new webpage is opened or refreshedJavascriptwindowobjectclearedSignal connection.
RelatedCodeAs follows:
Webkit_vlc.h
# Ifndef webkit_vlc_h
# Define webkit_vlc_h
# Include <qwidget>
# Include <qwebview>
# Include"Obj_openvlc.h"
NamespaceUi {
ClassWebkit_vlc;
}
ClassWebkit_vlc:PublicQwebview
{
Q_object
Public:
ExplicitWebkit_vlc (qwidget * parent = 0 );
~ Webkit_vlc ();
PrivateSlots:
VoidAddjavascriptobject ();
Private:
Ui: webkit_vlc * UI;
Obj_openvlc * obj_openvlc;// The object that will be exposed to Javascript
};
# Endif// Webkit_vlc_h
Webkit_vlc.cpp
# Include"Webkit_vlc.h"
# Include"Ui_webkit_vlc.h"
# Include <qwebpage>
# Include <qwebframe>
# Include <qtdebug>
Webkit_vlc: webkit_vlc (qwidget * parent ):
Qwebview (parent ),
UI (NewUi: webkit_vlc)
{
Setwindowflags (QT: framelesswindowhint );
UI-> setupui (This);
Resize (1280,720 );
Obj_openvlc =NewObj_openvlc (This);
Settings ()-> setattribute (qwebsettings: javascriptenabled,True);
Settings ()-> setattribute (qwebsettings: pluginsenabled,True);
Connect (page ()-> mainframe (), signal (javascriptwindowobjectcleared ()),
This, Slot (addjavascriptobject ()));
Load (qurl ("File: // home/nxx/webkit_vlc/view.html"));
}
Webkit_vlc ::~ Webkit_vlc ()
{
DeleteUi;
}
VoidWebkit_vlc: addjavascriptobject (){
Qdebug () <"Addjavascriptobject";
// Javascript can access the obj_openvlc object through the object name obj_open_vlc.
Page ()-> mainframe ()-> addtow.criptwindowobject ("Obj_open_vlc",This-> Obj_openvlc );
}
Now the object named "obj_open_vlc" has been exposed to WebKit JavaScript. This object name corresponds to a local class object.
Obj_openvlc * obj_openvlc.
The following code implements this class and defines a slot for WebKit JavaScript calls in the class.
Obj_openvlc.h
# Ifndef obj_openvlc_h
# Define obj_openvlc_h
# Include <qobject>
ClassObj_openvlc:PublicQobject
{
Q_object
Public:
ExplicitObj_openvlc (qobject * parent = 0 );
Signals:
PublicSlots:
// The slot for JavaScript call
Qstring openvlc (qstring URL );
};
# Endif// Obj_openvlc_h
Obj_openvlc.cpp
# Include"Obj_openvlc.h"
# Include <qtdebug>
Obj_openvlc: obj_openvlc (qobject * parent ):
Qobject (parent)
{
}
Qstring obj_openvlc: openvlc (qstring URL)
{
Qdebug () <"Open"<URL;
ReturnURL;
}
2. Use JavaScript to call the slot of the local qobject in HTML.
The HTML code is as follows:
View.html
<Html>
<Script Language="Jscript">
Function open ()
{
Try {
VaR url = obj_open_vlc.openvlc ("192.168.0.1/webroot/test. f4v ");
Alert (URL );
}
Catch (e ){
Alert (E );
}
}
</Script>
<Body>
<H1>Open VLC</H1>
<Input Type=Button Value="Open" Onclick="Open ()">
</Body>
</Html>
This HTML uses JavaScript code
Obj_open_vlc.openvlc ("192.168.0.1/webroot/test. f4v ");
The slot (method) of the object obj_openvlc of the local class obj_openvlc is called)Openvlc ().
In this way, the WebKit HTML webpage uses JavaScript to call the method of a local object.
2. QT local object calls the JavaScript method in HTML
Currently, I know two methods:
1. Through the qwebframe class slot:
Qvariant qwebframe: evaluatejavascript (const qstring & scriptsource)
To directly call methods in Javascript. For example, if I have a javascript loadfinished () method in the HTML webpage, you can directly call the method below:
Webview-> page ()-> mainframe ()-> evaluatejavascript ("loadfinished ();");
Here we assume that webview is an object of the qwebview class.
2. Connect the signal of the local object to the JavaScript method.
When this signal is sent locally, the JavaScript method is also called. However, you need to add code similar to the following in JavaScript.
// Connect the signal of the QT local object obj_open_vlc with the JavaScript function slotbuttonclicked
Obj_open_vlc.objsignal.connect (slotlinkclicked );
Here, objsignal is a signal of the QT local object obj_open_vlc, and slotbuttonclicked is a method in ipvcipt. Then, if the local obj_open_vlc object executes emit objsignal (), it will call the javascipt method.Slotlinkclicked ().
Here we paste the relevant HTML code:
<Html Xmlns=Http://www.w3.org/1999/xhtml">
<Title>Media Server</Title>
<Meta HTTP-Equiv="Content-Type" Content="Text/html; charset = UTF-8" />
<Script Language="JavaScript">
// Connect the signal of the QT local object obj_open_vlc with the JavaScript function slotbuttonclicked
Obj_open_vlc.objsignal.connect (slotlinkclicked );
Function slotlinkclicked ()
{
Alert ("link is clicked! ");
}
Function loadfinished ()
{
Alert ("Page load finished! ");
}
Function getrtmphost ()
{
If (location. Protocol = "http:" | location. Protocol = "https :")
Return "/" + location. hostname + "/";
Else
Return "";
}
Function open (file)
{
Url = 'rtmp: // '+ getrtmphost () + 'vod/' + File
Try {
VaR play = obj_open_vlc.openvlc (URL );
// Alert (play );
}
Catch (e ){
Alert (E );
}
}
</Script>
<Body>
<H1>Media Server</H1>
<P><A Href="Javascript: open ('test1. f4v ');">Test1.f4v</A></P>
<P><A Href="Javascript: open ('test2. f4v ');">Test2.f4v</A></P>
</Body>
</Html>