2011-04-06 Alexis Menard <alexis.menard@openbossa.org>
[WebKit-https.git] / Source / WebKit / qt / docs / webkitsnippets / qtwebkit_bridge_snippets.cpp
1
2 void wrapInFunction()
3 {
4
5     //! [0]
6     // ...
7     QWebFrame *frame = myWebPage->mainFrame();
8     frame->addToJavaScriptWindowObject("someNameForMyObject", myObject);
9     // ...
10     //! [0]
11 #if 0
12     //! [1]
13     {
14         width: ...,
15         height: ...,
16         toDataURL: function() { ... },
17         assignToHTMLImageElement: function(element) { ... }
18     }
19     //! [1]
20 #endif
21     //! [2]
22     class MyObject : QObject {
23         Q_OBJECT
24         Q_PROPERTY(QPixmap myPixmap READ getPixmap)
25
26     public:
27         QPixmap getPixmap() const;
28     };
29
30     /* ... */
31
32     MyObject myObject;
33     myWebPage.mainFrame()->addToJavaScriptWindowObject("myObject", &myObject);
34
35     //! [2]
36 #if 0
37     //! [3]
38     <html>
39         <head>
40             <script>
41                 function loadImage()
42                 {
43                     myObject.myPixmap.assignToHTMLImageElement(document.getElementById("imageElement"));
44                 }
45             </script>
46         </head>
47         <body onload="loadImage()">
48             <img id="imageElement" width="300" height="200" />
49         </body>
50     </html>
51 //! [3]
52 #endif
53 //! [4]
54 class MyObject : QObject {
55         Q_OBJECT
56
57     public slots:
58         void doSomethingWithWebElement(const QWebElement&);
59     };
60
61     /* ... */
62
63     MyObject myObject;
64     myWebPage.mainFrame()->addToJavaScriptWindowObject("myObject", &myObject);
65
66     //! [4]
67 #if 0
68     //! [5]
69     <html>
70          <head>
71              <script>
72                  function runExample() {
73                     myObject.doSomethingWithWebElement(document.getElementById("someElement"));
74                  }
75             </script>
76          </head>
77          <body onload="runExample()">
78              <span id="someElement">Text</span>
79          </body>
80      </html>
81     //! [5]
82     //! [6]
83     connect(function);
84     //! [6]
85     //! [7]
86     function myInterestingScriptFunction() { ... }
87     ...
88     myQObject.somethingChanged.connect(myInterestingScriptFunction);
89     //! [7]
90     //! [8]
91     myQObject.somethingChanged.connect(myOtherQObject.doSomething);
92     //! [8]
93     //! [9]
94     myQObject.somethingChanged.disconnect(myInterestingFunction);
95     myQObject.somethingChanged.disconnect(myOtherQObject.doSomething);
96     //! [9]
97     //! [10]
98     myQObject.somethingChanged.connect(thisObject, function)
99     //! [10]
100     //! [11]
101     var form = { x: 123 };
102     var onClicked = function() { print(this.x); };
103     myButton.clicked.connect(form, onClicked);
104     //! [11]
105     //! [12]
106     myQObject.somethingChanged.disconnect(thisObject, function);
107     //! [12]
108     //! [13]
109     connect(function);
110     //! [13]
111     //! [14]
112     myQObject.somethingChanged.connect(thisObject, "functionName")
113     //! [14]
114     //! [15]
115     var obj = { x: 123, fun: function() { print(this.x); } };
116     myQObject.somethingChanged.connect(obj, "fun");
117     //! [15]
118     //! [16]
119     connect(function);
120     //! [16]
121     //! [17]
122     myQObject.somethingChanged.disconnect(thisObject, "functionName");
123     //! [17]
124     //! [18]
125     try {
126         myQObject.somethingChanged.connect(myQObject, "slotThatDoesntExist");
127     } catch (e) {
128         print(e);
129     }
130     //! [18]
131     //! [19]
132     myQObject.somethingChanged("hello");
133     //! [19]
134     //! [20]
135     myQObject.myOverloadedSlot(10);   // will call the int overload
136     myQObject.myOverloadedSlot("10"); // will call the QString overload
137     //! [20]
138     //! [21]
139     myQObject['myOverloadedSlot(int)']("10");   // call int overload; the argument is converted to an int
140     myQObject['myOverloadedSlot(QString)'](10); // call QString overload; the argument is converted to a string
141     //! [21]
142     //! [22]
143     class MyObject : public QObject
144     {
145         Q_OBJECT
146
147     public:
148         Q_INVOKABLE void thisMethodIsInvokableInJavaScript();
149         void thisMethodIsNotInvokableInJavaScript();
150
151         ...
152     };
153     //! [22]
154     //! [23]
155         Q_PROPERTY(bool enabled READ enabled WRITE setEnabled)
156     //! [23]
157     //! [24]
158     myQObject.enabled = true;
159
160     ...
161
162     myQObject.enabled = !myQObject.enabled;
163     //! [24]
164     //! [25]
165     myDialog.okButton
166     //! [25]
167     //! [26]
168     myDialog.okButton
169     myDialog.okButton.objectName = "cancelButton";
170     // from now on, myDialog.cancelButton references the button
171     //! [26]
172 #endif
173 }
174