Open the following file in the QT directory:
/Src/3 rdparty/WebKit/javascriptcore/bindings/runtime. cpp
There are the following lines:
View plaincopy to clipboardprint?
- # If platform (QT)
- Case instance: qtlanguage :{
- Newinstance = bindings: qtinstance: getqtinstance (qobject *) nativeinstance, rootobject );
- Break;
- }
- # Endif
# If platform (QT) Case instance: qtlanguage: {newinstance = bindings: qtinstance: getqtinstance (qobject *) nativeinstance, rootobject); break ;}# endif
This indicates that you can use qtlanguage to write custom JavaScript objects. There are also a few other languages, that is, you can use them to write extended JavaScript objects, and you should be able to customize your own language. Since the qt gui is used, qtlanguage should be the most suitable.
Open the example file:
/Src/3 rdparty/WebKit/javascriptcore/bindings/testqtbindings. cpp
You can see the following lines:
View plaincopy to clipboardprint?
- Static char code [] =
- "Myinterface. Foo ();/N"
- "Myinterface. teststring =/" Hello/";/N"
- "Str = myinterface. teststring;/N"
- "Myinterface. testint = 10;/N"
- "I = myinterface. testint;/N ";
Static char code [] = "myinterface. foo ();/N "" myinterface. teststring =/"Hello/";/N "" str = myinterface. teststring;/N "" myinterface. testint = 10;/N "" I = myinterface. testint;/N ";
This means that the content of testqtbindings. cpp can parse the following javascript:
View plaincopy to clipboardprint?
- Myinterface. Foo ();
- Myinterface. teststring = "hello ";
- VaR STR = myinterface. teststring;
- Myinterface. testint = 10;
- VaR I = myinterface. testint;
Myinterface. Foo (); myinterface. teststring = "hello"; var STR = myinterface. teststring; myinterface. testint = 10; var I = myinterface. testint;
I want to define my own Javascript. For example, add a csdn object before the myinterface object:
View plaincopy to clipboardprint?
- Csdn. myinterface. Foo ();
- Csdn. myinterface. teststring = "hello ";
- VaR STR = csdn. myinterface. teststring;
Csdn. myinterface. Foo (); csdn. myinterface. teststring = "hello"; var STR = csdn. myinterface. teststring;
This is simple, but it is like csdn. myinterface. in this way, I did not find the connection between the csdn and myinterface objects on the Internet (Chinese and English websites) for a long time. I thought of the result myself.
1. Add another mycsdnobject object to the original file:
View plaincopy to clipboardprint?
- Class mycsdnobject: Public qobject
- {
- Q_object
- Q_property (qstring teststring read teststring write setteststring)
- Q_property (INT testint read testint write settestint)
- // Add the qobject member as the property of the csdn object and map it to the testmyinterface function.
- Q_property (qobject * myinterface read testmyinterface)
- Public:
- Mycsdnobject (): qobject (0), INTEGER (0), m_myobject (null ){}
- Void setteststring (const qstring & Str ){
- Qdebug () <"called setteststring" <STR;
- String = STR;
- }
- Void settestint (int I ){
- Qdebug () <"called settestint" <I;
- Integer = I;
- }
- Qstring teststring () const {
- Qdebug () <"called teststring" <string;
- Return string;
- }
- Int testint () const {
- Qdebug () <"called testint" <integer;
- Return integer;
- }
- // A qobject object is returned here.
- Qobject * testmyinterface ()
- {
- If (m_myobject = NULL)
- M_myobject = new myobject ();
- Return m_myobject;
- }
- Qstring string;
- Int integer;
- // Name a qobject object
- Myobject * m_myobject;
- Public slots:
- Void Foo () {qdebug () <"foo invoked ";}
- };
Class mycsdnobject: Public qobject {q_object q_property (qstring teststring read teststring write setteststring) q_property (INT testint read testint write settestint) // Add the qobject member as the property of the csdn object, ing to the testmyinterface function q_property (qobject * myinterface read testmyinterface) Public: mycsdnobject (): qobject (0), INTEGER (0), m_myobject (null) {} void setteststring (const qstring & Str) {qdebug () <"called setteststring" <STR; string = STR;} void settestint (int I) {qdebug () <"called settestint" <I; integer = I;} qstring teststring () const {qdebug () <"called teststring" <string; return string ;} int testint () const {qdebug () <"called testint" <integer; return integer;} // here, a qobject object qobject * testmyinterface () is returned () {If (m_myobject = NULL) m_myobject = new myobject (); Return m_myobject;} qstring string; int integer; // name a qobject object myobject * m_myobject; Public slots: void Foo () {qdebug () <"foo invoked ";}};
In this way, two objects, mycsdnobject and myobject, can be connected to csdn. testmyinterface.
2. Modify the name of the created root object.
View plaincopy to clipboardprint?
- Global-> put (exec, identifier ("csdn"), instance: createruntimeobject (instance: qtlanguage, (void *) myobject ));
Global-> put (exec, identifier ("csdn"), instance: createruntimeobject (instance: qtlanguage, (void *) myobject ));
In this way, you can. This is a test program. You need to add the test code to the WebKit browser. Open the file:
./Src/3 rdparty/WebKit/QT/API/qwebframe. h
Name the myobject class and mycsdnobject class, and then add the csdn root object as a private member of the qwebframe class, as shown below:
View plaincopy to clipboardprint?
- Class myobject: Public qobject
- {
- Q_object
- // Omit...
- };
- Class mycsdnobject: Public qobject
- {
- Q_object
- // Omit...
- };
- Class qwebkit_export qwebframe: Public qobject
- {
- Q_object
- Q_property (qreal textsizemultiplier read textsizemultiplier write settextsizemultiplier)
- Q_property (qstring title Read title)
- Q_property (qurl URL read URL write seturl)
- Q_property (qicon icon read icon)
- Q_property (qsize contentssize read contentssize)
- PRIVATE:
- Qwebframe (qwebpage * parent, qwebframedata * framedata );
- Qwebframe (qwebframe * parent, qwebframedata * framedata );
- ~ Qwebframe ();
- // Name csdn root object
- Mycsdnobject * m_csdn;
- Public:
- // The following content is omitted...
- };
Class myobject: Public qobject {q_object // omitting ...}; class mycsdnobject: Public qobject {q_object // omitting ...}; class qwebkit_export qwebframe: Public qobject {q_object q_property (qreal textsizemultiplier read writable write failed) q_property (qstring title Read title) q_property (qurl URL read URL write seturl) q_property) q_property (qsize contentssize R EAD contentssize) PRIVATE: qwebframe (qwebpage * parent, qwebframedata * framedata); qwebframe (qwebframe * parent, qwebframedata * framedata );~ Qwebframe (); // The root object mycsdnobject * m_csdn; public: // The following is omitted ...};
Open the qwebframe. cpp file and create the csdn root object in the qwebframe constructor.
View plaincopy to clipboardprint?
- Qwebframe: qwebframe (qwebpage * parent, qwebframedata * framedata)
- : Qobject (parent)
- , D (New qwebframeprivate)
- {
- D-> page = parent;
- D-> Init (this, parent-> D-> page, framedata );
- // Create the csdn root object here
- {
- KJS: bindings: rootobject * root = D-> frame-> bindingrootobject ();
- KJS: execstate * exec = root-> interpreter ()-> globalexec ();
- KJS: jsobject * Global = root-> interpreter ()-> globalobject ();
- M_csdn = new mycsdnobject ();
- Global-> put (exec, KJS: identifier ("csdn"), KJS: bindings: instance: createruntimeobject (KJS: bindings: instance: qtlanguage, (void *) m_csdn, root ));
- }
- If (! Framedata-> URL. isempty ()){
- Resourcerequest request (framedata-> URL, framedata-> referrer );
- D-> frame-> loader ()-> load (request, framedata-> name );
- }
- }
Qwebframe: qwebframe (qwebpage * parent, qwebframedata * framedata): qobject (parent), D (New qwebframeprivate) {d-> page = parent; D-> Init (this, parent-> D-> page, framedata); // create the csdn root object {KJS: bindings: rootobject * root = D-> frame-> bindingrootobject (); KJS: execstate * exec = root-> interpreter ()-> globalexec (); KJS: jsobject * Global = root-> interpreter ()-> globalobject (); m_csdn = new mycsdnobject ( ); Global-> put (exec, KJS: identifier ("csdn"), KJS: bindings: instance: createruntimeobject (KJS: bindings: instance :: qtlanguage, (void *) m_csdn, root);} If (! Framedata-> URL. isempty () {resourcerequest request (framedata-> URL, framedata-> referrer); D-> frame-> loader ()-> load (request, framedata-> name );}}
Delete the csdn root object in the destructor of qwebframe
View plaincopy to clipboardprint?
- Qwebframe ::~ Qwebframe ()
- {
- If (D-> frame & D-> frame-> loader ()-> client ())
- Static_cast (D-> frame-> loader ()-> client ()-> m_webframe = 0;
- If (m_csdn)
- {
- Delete m_csdn;
- M_csdn = NULL;
- }
- Delete D;
- }
Qwebframe ::~ Qwebframe () {If (D-> frame & D-> frame-> loader ()-> client ()) static_cast (D-> frame-> loader ()-> client ()-> m_webframe = 0; If (m_csdn) {Delete m_csdn; m_csdn = NULL;} Delete D ;}
Then, write an HTML test page csdn1.htm. The content is as follows:
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"> <HTML> <Head> <Title> new document </title> <Script language = "JavaScript"> <! -- Function Test () { Csdn. myinterface. Foo (); Csdn. myinterface. teststring = "Hello qtwebkit JavaScript "; VaR STR = csdn. myinterface. teststring; Document. getelementbyid ("txtmessage"). innerhtml = STR; } // --> </SCRIPT> </Head> <Body> <P> <a href = "javascript: Test ();"> test QT-WebKit JavaScript </a> </P> <P id = "txtmessage"> </P> </Body> </Html> |
Compile and run the WebKit browser, open the test page csdn1.htm, and click the test QT-WebKit JavaScript link above to get the following results:
The above Hello qtwebkit Javascript is the content of csdn. myinterface. teststring set by JavaScript.