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