114 QT_BEGIN_NAMESPACE |
113 QT_BEGIN_NAMESPACE |
115 |
114 |
116 /*! |
115 /*! |
117 \qmlclass QtObject QObject |
116 \qmlclass QtObject QObject |
118 \since 4.7 |
117 \since 4.7 |
119 \brief The QtObject element is the most basic element in QML |
118 \brief The QtObject element is the most basic element in QML. |
120 |
119 |
121 The QtObject element is a non-visual element which contains only the |
120 The QtObject element is a non-visual element which contains only the |
122 objectName property. It is useful for when you need an extremely |
121 objectName property. |
123 lightweight element to place your own custom properties in. |
122 |
|
123 It can be useful to create a QtObject if you need an extremely |
|
124 lightweight element to enclose a set of custom properties: |
|
125 |
|
126 \snippet doc/src/snippets/declarative/qtobject.qml 0 |
124 |
127 |
125 It can also be useful for C++ integration, as it is just a plain |
128 It can also be useful for C++ integration, as it is just a plain |
126 QObject. See the QObject documentation for further details. |
129 QObject. See the QObject documentation for further details. |
127 */ |
130 */ |
128 /*! |
131 /*! |
129 \qmlproperty string QtObject::objectName |
132 \qmlproperty string QML:QtObject::objectName |
130 This property allows you to give a name to this specific object instance. |
133 This property holds the QObject::objectName for this specific object instance. |
131 |
134 |
132 See \l{scripting.html#accessing-child-qobjects}{Accessing Child QObjects} |
135 This allows a C++ application to locate an item within a QML component |
133 in the scripting documentation for details how objectName can be used from |
136 using the QObject::findChild() method. For example, the following C++ |
134 scripts. |
137 application locates the child \l Rectangle item and dynamically changes its |
|
138 \c color value: |
|
139 |
|
140 \qml |
|
141 // MyRect.qml |
|
142 |
|
143 import Qt 4.7 |
|
144 |
|
145 Item { |
|
146 width: 200; height: 200 |
|
147 |
|
148 Rectangle { |
|
149 anchors.fill: parent |
|
150 color: "red" |
|
151 objectName: "myRect" |
|
152 } |
|
153 } |
|
154 \endqml |
|
155 |
|
156 \code |
|
157 // main.cpp |
|
158 |
|
159 QDeclarativeView view; |
|
160 view.setSource(QUrl::fromLocalFile("MyRect.qml")); |
|
161 view.show(); |
|
162 |
|
163 QDeclarativeItem *item = view.rootObject()->findChild<QDeclarativeItem*>("myRect"); |
|
164 if (item) |
|
165 item->setProperty("color", QColor(Qt::yellow)); |
|
166 \endcode |
135 */ |
167 */ |
136 |
168 |
137 struct StaticQtMetaObject : public QObject |
169 struct StaticQtMetaObject : public QObject |
138 { |
170 { |
139 static const QMetaObject *get() |
171 static const QMetaObject *get() |
150 |
182 |
151 qmlRegisterType<QDeclarativeBinding>(); |
183 qmlRegisterType<QDeclarativeBinding>(); |
152 } |
184 } |
153 |
185 |
154 /*! |
186 /*! |
|
187 \keyword QmlGlobalQtObject |
155 \qmlclass Qt QDeclarativeEnginePrivate |
188 \qmlclass Qt QDeclarativeEnginePrivate |
156 \brief The QML Global Qt Object |
189 \brief The QML global Qt object provides useful enums and functions from Qt. |
157 |
190 |
158 The Qt object provides useful enums and functions from Qt, for use in all QML |
191 The \c Qt object provides useful enums and functions from Qt, for use in all QML files. |
159 files. Note that you do note create instances of this type, but instead use |
192 |
160 the members of the global "Qt" object. |
193 The \c Qt object is not a QML element; it cannot be instantiated. It is a global object |
|
194 with enums and functions. To use it, call the members of the global \c Qt object directly. |
|
195 For example: |
|
196 |
|
197 \qml |
|
198 import Qt 4.7 |
|
199 |
|
200 Text { |
|
201 color: Qt.rgba(255, 0, 0, 1) |
|
202 text: Qt.md5("hello, world") |
|
203 } |
|
204 \endqml |
|
205 |
161 |
206 |
162 \section1 Enums |
207 \section1 Enums |
163 |
208 |
164 The Qt object contains all enums in the Qt namespace. For example, you can |
209 The Qt object contains all enums in the Qt namespace. For example, you can |
165 access the AlignLeft member of the Qt::AlignmentFlag enum with \c Qt.AlignLeft. |
210 access the \c AlignLeft member of the \c Qt::AlignmentFlag enum with \c Qt.AlignLeft. |
166 |
211 |
167 For a full list of enums, see the \l{Qt Namespace} documentation. |
212 For a full list of enums, see the \l{Qt Namespace} documentation. |
168 |
213 |
169 \section1 Types |
214 \section1 Types |
170 The Qt object also contains helper functions for creating objects of specific |
215 The Qt object also contains helper functions for creating objects of specific |
171 data types. This is primarily useful when setting the properties of an item |
216 data types. This is primarily useful when setting the properties of an item |
172 when the property has one of the following types: |
217 when the property has one of the following types: |
173 |
218 |
174 \list |
219 \list |
175 \o Color |
220 \o \c color - use \l{QML:Qt::rgba()}{Qt.rgba()}, \l{QML:Qt::hsla()}{Qt.hsla()}, \l{QML:Qt::darker()}{Qt.darker()}, \l{QML:Qt::lighter()}{Qt.lighter()} or \l{QML:Qt::tint()}{Qt.tint()} |
176 \o Rect |
221 \o \c rect - use \l{QML:Qt::rect()}{Qt.rect()} |
177 \o Point |
222 \o \c point - use \l{QML:Qt::point()}{Qt.point()} |
178 \o Size |
223 \o \c size - use \l{QML:Qt::size()}{Qt.size()} |
179 \o Vector3D |
224 \o \c vector3d - use \l{QML:Qt::vector3d()}{Qt.vector3d()} |
180 \endlist |
225 \endlist |
181 |
226 |
182 There are also string based constructors for these types, see \l{qdeclarativebasictypes.html}{Qml Types}. |
227 There are also string based constructors for these types. See \l{qdeclarativebasictypes.html}{QML Basic Types} for more information. |
183 |
228 |
184 \section1 Date/Time Formatters |
229 \section1 Date/Time Formatters |
185 |
230 |
186 The Qt object contains several functions for formatting dates and times. |
231 The Qt object contains several functions for formatting dates and times. |
187 |
232 |
188 \list |
233 \list |
189 \o \l{Qt::formatDateTime}{string Qt.formatDateTime(datetime date, variant format) |
234 \o \l{QML:Qt::formatDateTime}{string Qt.formatDateTime(datetime date, variant format)} |
190 \o \l{Qt::formatDate}{string Qt.formatDate(datetime date, variant format) |
235 \o \l{QML:Qt::formatDate}{string Qt.formatDate(datetime date, variant format)} |
191 \o \l{Qt::formatTime}{string Qt.formatTime(datetime date, variant format) |
236 \o \l{QML:Qt::formatTime}{string Qt.formatTime(datetime date, variant format)} |
192 \endlist |
237 \endlist |
193 |
238 |
194 The format specification is described at \l{Qt::formatDateTime}{Qt.formatDateTime}. |
239 The format specification is described at \l{QML:Qt::formatDateTime}{Qt.formatDateTime}. |
195 |
240 |
196 |
241 |
197 \section1 Dynamic Object Creation |
242 \section1 Dynamic Object Creation |
198 The following functions on the global object allow you to dynamically create QML |
243 The following functions on the global object allow you to dynamically create QML |
199 items from files or strings. See \l{Dynamic Object Management} for an overview |
244 items from files or strings. See \l{Dynamic Object Management} for an overview |
200 of their use. |
245 of their use. |
201 |
246 |
202 \list |
247 \list |
203 \o \l{Qt::createComponent}{object Qt.createComponent(url)} |
248 \o \l{QML:Qt::createComponent()}{object Qt.createComponent(url)} |
204 \o \l{Qt::createQmlObject}{object Qt.createQmlObject(string qml, object parent, string filepath)} |
249 \o \l{QML:Qt::createQmlObject()}{object Qt.createQmlObject(string qml, object parent, string filepath)} |
205 \endlist |
250 \endlist |
206 */ |
251 */ |
207 |
252 |
208 |
253 |
209 QDeclarativeEnginePrivate::QDeclarativeEnginePrivate(QDeclarativeEngine *e) |
254 QDeclarativeEnginePrivate::QDeclarativeEnginePrivate(QDeclarativeEngine *e) |
210 : captureProperties(false), rootContext(0), currentExpression(0), isDebugging(false), |
255 : captureProperties(false), rootContext(0), isDebugging(false), |
211 outputWarningsToStdErr(true), contextClass(0), sharedContext(0), sharedScope(0), |
256 outputWarningsToStdErr(true), contextClass(0), sharedContext(0), sharedScope(0), |
212 objectClass(0), valueTypeClass(0), globalClass(0), cleanup(0), erroredBindings(0), |
257 objectClass(0), valueTypeClass(0), globalClass(0), cleanup(0), erroredBindings(0), |
213 inProgressCreations(0), scriptEngine(this), workerScriptEngine(0), componentAttached(0), |
258 inProgressCreations(0), scriptEngine(this), workerScriptEngine(0), componentAttached(0), |
214 inBeginCreate(false), networkAccessManager(0), networkAccessManagerFactory(0), |
259 inBeginCreate(false), networkAccessManager(0), networkAccessManagerFactory(0), |
215 typeManager(e), importDatabase(e), uniqueId(1) |
260 typeManager(e), importDatabase(e), uniqueId(1) |
283 qtObject.setProperty(QLatin1String("lighter"), newFunction(QDeclarativeEnginePrivate::lighter, 1)); |
327 qtObject.setProperty(QLatin1String("lighter"), newFunction(QDeclarativeEnginePrivate::lighter, 1)); |
284 qtObject.setProperty(QLatin1String("darker"), newFunction(QDeclarativeEnginePrivate::darker, 1)); |
328 qtObject.setProperty(QLatin1String("darker"), newFunction(QDeclarativeEnginePrivate::darker, 1)); |
285 qtObject.setProperty(QLatin1String("tint"), newFunction(QDeclarativeEnginePrivate::tint, 2)); |
329 qtObject.setProperty(QLatin1String("tint"), newFunction(QDeclarativeEnginePrivate::tint, 2)); |
286 } |
330 } |
287 |
331 |
|
332 #ifndef QT_NO_TEXTDATE |
288 //date/time formatting |
333 //date/time formatting |
289 qtObject.setProperty(QLatin1String("formatDate"),newFunction(QDeclarativeEnginePrivate::formatDate, 2)); |
334 qtObject.setProperty(QLatin1String("formatDate"),newFunction(QDeclarativeEnginePrivate::formatDate, 2)); |
290 qtObject.setProperty(QLatin1String("formatTime"),newFunction(QDeclarativeEnginePrivate::formatTime, 2)); |
335 qtObject.setProperty(QLatin1String("formatTime"),newFunction(QDeclarativeEnginePrivate::formatTime, 2)); |
291 qtObject.setProperty(QLatin1String("formatDateTime"),newFunction(QDeclarativeEnginePrivate::formatDateTime, 2)); |
336 qtObject.setProperty(QLatin1String("formatDateTime"),newFunction(QDeclarativeEnginePrivate::formatDateTime, 2)); |
|
337 #endif |
292 |
338 |
293 //misc methods |
339 //misc methods |
294 qtObject.setProperty(QLatin1String("openUrlExternally"),newFunction(QDeclarativeEnginePrivate::desktopOpenUrl, 1)); |
340 qtObject.setProperty(QLatin1String("openUrlExternally"),newFunction(QDeclarativeEnginePrivate::desktopOpenUrl, 1)); |
295 qtObject.setProperty(QLatin1String("fontFamilies"),newFunction(QDeclarativeEnginePrivate::fontFamilies, 0)); |
341 qtObject.setProperty(QLatin1String("fontFamilies"),newFunction(QDeclarativeEnginePrivate::fontFamilies, 0)); |
296 qtObject.setProperty(QLatin1String("md5"),newFunction(QDeclarativeEnginePrivate::md5, 1)); |
342 qtObject.setProperty(QLatin1String("md5"),newFunction(QDeclarativeEnginePrivate::md5, 1)); |
606 } |
652 } |
607 |
653 |
608 /*! |
654 /*! |
609 |
655 |
610 Sets the \a provider to use for images requested via the \e |
656 Sets the \a provider to use for images requested via the \e |
611 image: url scheme, with host \a providerId. |
657 image: url scheme, with host \a providerId. The QDeclarativeEngine |
612 |
658 takes ownership of \a provider. |
613 QDeclarativeImageProvider allows images to be provided to QML |
659 |
614 asynchronously. The image request will be run in a low priority |
660 Image providers enable support for pixmap and threaded image |
615 thread. This allows potentially costly image loading to be done in |
661 requests. See the QDeclarativeImageProvider documentation for details on |
616 the background, without affecting the performance of the UI. |
662 implementing and using image providers. |
617 |
663 |
618 Note that images loaded from a QDeclarativeImageProvider are cached |
664 Note that images loaded from a QDeclarativeImageProvider are cached |
619 by QPixmapCache, similar to any image loaded by QML. |
665 by QPixmapCache, similar to any image loaded by QML. |
620 |
|
621 The QDeclarativeEngine assumes ownership of the provider. |
|
622 |
|
623 This example creates a provider with id \e colors: |
|
624 |
|
625 \snippet examples/declarative/cppextensions/imageprovider/imageprovider.cpp 0 |
|
626 |
|
627 \snippet examples/declarative/cppextensions/imageprovider/imageprovider-example.qml 0 |
|
628 |
666 |
629 \sa removeImageProvider() |
667 \sa removeImageProvider() |
630 */ |
668 */ |
631 void QDeclarativeEngine::addImageProvider(const QString &providerId, QDeclarativeImageProvider *provider) |
669 void QDeclarativeEngine::addImageProvider(const QString &providerId, QDeclarativeImageProvider *provider) |
632 { |
670 { |
655 void QDeclarativeEngine::removeImageProvider(const QString &providerId) |
693 void QDeclarativeEngine::removeImageProvider(const QString &providerId) |
656 { |
694 { |
657 Q_D(QDeclarativeEngine); |
695 Q_D(QDeclarativeEngine); |
658 QMutexLocker locker(&d->mutex); |
696 QMutexLocker locker(&d->mutex); |
659 delete d->imageProviders.take(providerId); |
697 delete d->imageProviders.take(providerId); |
|
698 } |
|
699 |
|
700 QDeclarativeImageProvider::ImageType QDeclarativeEnginePrivate::getImageProviderType(const QUrl &url) |
|
701 { |
|
702 QMutexLocker locker(&mutex); |
|
703 QDeclarativeImageProvider *provider = imageProviders.value(url.host()); |
|
704 if (provider) |
|
705 return provider->imageType(); |
|
706 return static_cast<QDeclarativeImageProvider::ImageType>(-1); |
660 } |
707 } |
661 |
708 |
662 QImage QDeclarativeEnginePrivate::getImageFromProvider(const QUrl &url, QSize *size, const QSize& req_size) |
709 QImage QDeclarativeEnginePrivate::getImageFromProvider(const QUrl &url, QSize *size, const QSize& req_size) |
663 { |
710 { |
664 QMutexLocker locker(&mutex); |
711 QMutexLocker locker(&mutex); |
665 QImage image; |
712 QImage image; |
666 QDeclarativeImageProvider *provider = imageProviders.value(url.host()); |
713 QDeclarativeImageProvider *provider = imageProviders.value(url.host()); |
667 if (provider) |
714 if (provider) |
668 image = provider->request(url.path().mid(1), size, req_size); |
715 image = provider->requestImage(url.path().mid(1), size, req_size); |
669 return image; |
716 return image; |
|
717 } |
|
718 |
|
719 QPixmap QDeclarativeEnginePrivate::getPixmapFromProvider(const QUrl &url, QSize *size, const QSize& req_size) |
|
720 { |
|
721 QMutexLocker locker(&mutex); |
|
722 QPixmap pixmap; |
|
723 QDeclarativeImageProvider *provider = imageProviders.value(url.host()); |
|
724 if (provider) |
|
725 pixmap = provider->requestPixmap(url.path().mid(1), size, req_size); |
|
726 return pixmap; |
670 } |
727 } |
671 |
728 |
672 /*! |
729 /*! |
673 Return the base URL for this engine. The base URL is only used to |
730 Return the base URL for this engine. The base URL is only used to |
674 resolve components when a relative URL is passed to the |
731 resolve components when a relative URL is passed to the |
1033 } |
1101 } |
1034 |
1102 |
1035 /*! |
1103 /*! |
1036 \qmlmethod object Qt::createComponent(url) |
1104 \qmlmethod object Qt::createComponent(url) |
1037 |
1105 |
1038 This function takes the URL of a QML file as its only argument. It returns |
1106 Returns a \l Component object created using the QML file at the specified \a url, |
1039 a component object which can be used to create and load that QML file. |
1107 or \c null if there was an error in creating the component. |
1040 |
1108 |
1041 Here is an example. Remember that QML files that might be loaded |
1109 Call \l {Component::createObject()}{Component.createObject()} on the returned |
1042 over the network cannot be expected to be ready immediately. |
1110 component to create an object instance of the component. |
1043 |
1111 |
1044 \snippet doc/src/snippets/declarative/componentCreation.js 0 |
1112 Here is an example. Notice it checks whether the component \l{Component::status}{status} is |
|
1113 \c Component.Ready before calling \l {Component::createObject()}{createObject()} |
|
1114 in case the QML file is loaded over a network and thus is not ready immediately. |
|
1115 |
|
1116 \snippet doc/src/snippets/declarative/componentCreation.js vars |
1045 \codeline |
1117 \codeline |
1046 \snippet doc/src/snippets/declarative/componentCreation.js 1 |
1118 \snippet doc/src/snippets/declarative/componentCreation.js func |
1047 |
1119 \snippet doc/src/snippets/declarative/componentCreation.js remote |
1048 If you are certain the files will be local, you could simplify to: |
1120 \snippet doc/src/snippets/declarative/componentCreation.js func-end |
1049 |
1121 \codeline |
1050 \snippet doc/src/snippets/declarative/componentCreation.js 2 |
1122 \snippet doc/src/snippets/declarative/componentCreation.js finishCreation |
1051 |
1123 |
1052 The methods and properties of the \l {Component} element are defined in its own |
1124 If you are certain the QML file to be loaded is a local file, you could omit the \c finishCreation() |
1053 page, but when using it dynamically only two methods are usually used. |
1125 function and call \l {Component::createObject()}{createObject()} immediately: |
1054 \l {Component::createObject()}{Component.createObject()} returns the created object or \c null if there is an error. |
1126 |
1055 If there is an error, \l {Component::errorString()}{Component.errorString()} describes |
1127 \snippet doc/src/snippets/declarative/componentCreation.js func |
1056 the error that occurred. Note that createObject() takes exactly one argument, which is set |
1128 \snippet doc/src/snippets/declarative/componentCreation.js local |
1057 to the parent of the created object. Graphical objects without a parent will not appear |
1129 \snippet doc/src/snippets/declarative/componentCreation.js func-end |
1058 on the scene, but if you do not wish to parent the item at this point you can safely pass |
1130 |
1059 in null. |
1131 To create a QML object from an arbitrary string of QML (instead of a file), |
1060 |
1132 use \l{QML:Qt::createQmlObject()}{Qt.createQmlObject()}. |
1061 If you want to just create an arbitrary string of QML, instead of |
|
1062 loading a QML file, consider the \l{Qt.createQmlObject(string qml, object parent, string filepath)}{Qt.createQmlObject()} function. |
|
1063 */ |
1133 */ |
1064 |
1134 |
1065 QScriptValue QDeclarativeEnginePrivate::createComponent(QScriptContext *ctxt, QScriptEngine *engine) |
1135 QScriptValue QDeclarativeEnginePrivate::createComponent(QScriptContext *ctxt, QScriptEngine *engine) |
1066 { |
1136 { |
1067 QDeclarativeEnginePrivate *activeEnginePriv = |
1137 QDeclarativeEnginePrivate *activeEnginePriv = |
1068 static_cast<QDeclarativeScriptEngine*>(engine)->p; |
1138 static_cast<QDeclarativeScriptEngine*>(engine)->p; |
1069 QDeclarativeEngine* activeEngine = activeEnginePriv->q_func(); |
1139 QDeclarativeEngine* activeEngine = activeEnginePriv->q_func(); |
1070 |
1140 |
1071 QDeclarativeContextData* context = activeEnginePriv->getContext(ctxt); |
|
1072 Q_ASSERT(context); |
|
1073 |
|
1074 if(ctxt->argumentCount() != 1) { |
1141 if(ctxt->argumentCount() != 1) { |
1075 return ctxt->throwError(QLatin1String("Qt.createComponent(): Invalid arguments")); |
1142 return ctxt->throwError(QLatin1String("Qt.createComponent(): Invalid arguments")); |
1076 }else{ |
1143 } else { |
|
1144 |
1077 QString arg = ctxt->argument(0).toString(); |
1145 QString arg = ctxt->argument(0).toString(); |
1078 if (arg.isEmpty()) |
1146 if (arg.isEmpty()) |
1079 return engine->nullValue(); |
1147 return engine->nullValue(); |
1080 QUrl url = QUrl(context->resolvedUrl(QUrl(arg))); |
1148 QUrl url; |
|
1149 QDeclarativeContextData* context = activeEnginePriv->getContext(ctxt); |
|
1150 if (context) |
|
1151 url = QUrl(context->resolvedUrl(QUrl(arg))); |
|
1152 else |
|
1153 url = activeEnginePriv->getUrl(ctxt).resolved(QUrl(arg)); |
1081 QDeclarativeComponent *c = new QDeclarativeComponent(activeEngine, url, activeEngine); |
1154 QDeclarativeComponent *c = new QDeclarativeComponent(activeEngine, url, activeEngine); |
1082 QDeclarativeComponentPrivate::get(c)->creationContext = context; |
1155 QDeclarativeComponentPrivate::get(c)->creationContext = context; |
1083 QDeclarativeData::get(c, true)->setImplicitDestructible(); |
1156 QDeclarativeData::get(c, true)->setImplicitDestructible(); |
1084 return activeEnginePriv->objectClass->newQObject(c, qMetaTypeId<QDeclarativeComponent*>()); |
1157 return activeEnginePriv->objectClass->newQObject(c, qMetaTypeId<QDeclarativeComponent*>()); |
1085 } |
1158 } |
1086 } |
1159 } |
1087 |
1160 |
1088 /*! |
1161 /*! |
1089 \qmlmethod object Qt::createQmlObject(string qml, object parent, string filepath) |
1162 \qmlmethod object Qt::createQmlObject(string qml, object parent, string filepath) |
1090 |
1163 |
1091 Creates a new object from the specified string of QML. It requires a |
1164 Returns a new object created from the given \a string of QML which will have the specified \a parent, |
1092 second argument, which is the id of an existing QML object to use as |
1165 or \c null if there was an error in creating the object. |
1093 the new object's parent. If a third argument is provided, this is used |
1166 |
1094 for error reporting as the filepath that the QML came from. |
1167 If \a filepath is specified, it will be used for error reporting for the created object. |
1095 |
1168 |
1096 Example (where \c targetItem is the id of an existing QML item): |
1169 Example (where \c parentItem is the id of an existing QML item): |
1097 |
1170 |
1098 \snippet doc/src/snippets/declarative/createQmlObject.qml 0 |
1171 \snippet doc/src/snippets/declarative/createQmlObject.qml 0 |
1099 |
1172 |
1100 This function is intended for use inside QML only. It is intended to behave |
1173 In the case of an error, a QtScript Error object is thrown. This object has an additional property, |
1101 similarly to eval, but for creating QML elements. |
1174 \c qmlErrors, which is an array of the errors encountered. |
1102 |
1175 Each object in this array has the members \c lineNumber, \c columnNumber, \c fileName and \c message. |
1103 Returns the created object, \c or null if there is an error. In the case of an |
|
1104 error, a QtScript Error object is thrown. This object has the additional property, |
|
1105 qmlErrors, which is an array of all the errors encountered when trying to execute the |
|
1106 QML. Each object in the array has the members \c lineNumber, \c columnNumber, \c fileName and \c message. |
|
1107 |
1176 |
1108 Note that this function returns immediately, and therefore may not work if |
1177 Note that this function returns immediately, and therefore may not work if |
1109 the QML loads new components. If you are trying to load a new component, |
1178 the \a qml string loads new components (that is, external QML files that have not yet been loaded). |
1110 for example from a QML file, consider the \l{Qt.createComponent(url file)}{Qt.createComponent()} function |
1179 If this is the case, consider using \l{QML:Qt::createComponent()}{Qt.createComponent()} instead. |
1111 instead. 'New components' refers to external QML files that have not yet |
|
1112 been loaded, and so it is safe to use \c Qt.createQmlObject() to load built-in |
|
1113 components. |
|
1114 */ |
1180 */ |
1115 |
1181 |
1116 QScriptValue QDeclarativeEnginePrivate::createQmlObject(QScriptContext *ctxt, QScriptEngine *engine) |
1182 QScriptValue QDeclarativeEnginePrivate::createQmlObject(QScriptContext *ctxt, QScriptEngine *engine) |
1117 { |
1183 { |
1118 QDeclarativeEnginePrivate *activeEnginePriv = |
1184 QDeclarativeEnginePrivate *activeEnginePriv = |
1229 return QDeclarativeEnginePrivate::get(engine)->scriptValueFromVariant(qVariantFromValue(QVector3D(x, y, z))); |
1297 return QDeclarativeEnginePrivate::get(engine)->scriptValueFromVariant(qVariantFromValue(QVector3D(x, y, z))); |
1230 } |
1298 } |
1231 |
1299 |
1232 /*! |
1300 /*! |
1233 \qmlmethod string Qt::formatDate(datetime date, variant format) |
1301 \qmlmethod string Qt::formatDate(datetime date, variant format) |
1234 This function returns the string representation of \c date, formatted according to \c format. |
1302 Returns the string representation of \c date, formatted according to \c format. |
1235 */ |
1303 */ |
|
1304 #ifndef QT_NO_TEXTDATE |
1236 QScriptValue QDeclarativeEnginePrivate::formatDate(QScriptContext*ctxt, QScriptEngine*engine) |
1305 QScriptValue QDeclarativeEnginePrivate::formatDate(QScriptContext*ctxt, QScriptEngine*engine) |
1237 { |
1306 { |
1238 int argCount = ctxt->argumentCount(); |
1307 int argCount = ctxt->argumentCount(); |
1239 if(argCount == 0 || argCount > 2) |
1308 if(argCount == 0 || argCount > 2) |
1240 return ctxt->throwError(QLatin1String("Qt.formatDate(): Invalid arguments")); |
1309 return ctxt->throwError(QLatin1String("Qt.formatDate(): Invalid arguments")); |
1241 |
1310 |
1242 QDate date = ctxt->argument(0).toDateTime().date(); |
1311 QDate date = ctxt->argument(0).toDateTime().date(); |
1243 Qt::DateFormat enumFormat = Qt::DefaultLocaleShortDate; |
1312 Qt::DateFormat enumFormat = Qt::DefaultLocaleShortDate; |
1244 if (argCount == 2) { |
1313 if (argCount == 2) { |
1245 if (ctxt->argument(1).isString()) { |
1314 QScriptValue formatArg = ctxt->argument(1); |
1246 QString format = ctxt->argument(1).toString(); |
1315 if (formatArg.isString()) { |
|
1316 QString format = formatArg.toString(); |
1247 return engine->newVariant(qVariantFromValue(date.toString(format))); |
1317 return engine->newVariant(qVariantFromValue(date.toString(format))); |
1248 } else if (ctxt->argument(1).isNumber()) { |
1318 } else if (formatArg.isNumber()) { |
1249 enumFormat = Qt::DateFormat(ctxt->argument(1).toUInt32()); |
1319 enumFormat = Qt::DateFormat(formatArg.toUInt32()); |
1250 } else { |
1320 } else { |
1251 return ctxt->throwError(QLatin1String("Qt.formatDate(): Invalid date format")); |
1321 return ctxt->throwError(QLatin1String("Qt.formatDate(): Invalid date format")); |
1252 } |
1322 } |
1253 } |
1323 } |
1254 return engine->newVariant(qVariantFromValue(date.toString(enumFormat))); |
1324 return engine->newVariant(qVariantFromValue(date.toString(enumFormat))); |
1255 } |
1325 } |
1256 |
1326 |
1257 /*! |
1327 /*! |
1258 \qmlmethod string Qt::formatTime(datetime time, variant format) |
1328 \qmlmethod string Qt::formatTime(datetime time, variant format) |
1259 This function returns the string representation of \c time, formatted according to \c format. |
1329 Returns the string representation of \c time, formatted according to \c format. |
1260 |
1330 |
1261 See Qt::formatDateTime for how to define \c format. |
1331 See Qt::formatDateTime for how to define \c format. |
1262 */ |
1332 */ |
1263 QScriptValue QDeclarativeEnginePrivate::formatTime(QScriptContext*ctxt, QScriptEngine*engine) |
1333 QScriptValue QDeclarativeEnginePrivate::formatTime(QScriptContext*ctxt, QScriptEngine*engine) |
1264 { |
1334 { |
1267 return ctxt->throwError(QLatin1String("Qt.formatTime(): Invalid arguments")); |
1337 return ctxt->throwError(QLatin1String("Qt.formatTime(): Invalid arguments")); |
1268 |
1338 |
1269 QTime date = ctxt->argument(0).toDateTime().time(); |
1339 QTime date = ctxt->argument(0).toDateTime().time(); |
1270 Qt::DateFormat enumFormat = Qt::DefaultLocaleShortDate; |
1340 Qt::DateFormat enumFormat = Qt::DefaultLocaleShortDate; |
1271 if (argCount == 2) { |
1341 if (argCount == 2) { |
1272 if (ctxt->argument(1).isString()) { |
1342 QScriptValue formatArg = ctxt->argument(1); |
1273 QString format = ctxt->argument(1).toString(); |
1343 if (formatArg.isString()) { |
|
1344 QString format = formatArg.toString(); |
1274 return engine->newVariant(qVariantFromValue(date.toString(format))); |
1345 return engine->newVariant(qVariantFromValue(date.toString(format))); |
1275 } else if (ctxt->argument(1).isNumber()) { |
1346 } else if (formatArg.isNumber()) { |
1276 enumFormat = Qt::DateFormat(ctxt->argument(1).toUInt32()); |
1347 enumFormat = Qt::DateFormat(formatArg.toUInt32()); |
1277 } else { |
1348 } else { |
1278 return ctxt->throwError(QLatin1String("Qt.formatTime(): Invalid time format")); |
1349 return ctxt->throwError(QLatin1String("Qt.formatTime(): Invalid time format")); |
1279 } |
1350 } |
1280 } |
1351 } |
1281 return engine->newVariant(qVariantFromValue(date.toString(enumFormat))); |
1352 return engine->newVariant(qVariantFromValue(date.toString(enumFormat))); |
1282 } |
1353 } |
1283 |
1354 |
1284 /*! |
1355 /*! |
1285 \qmlmethod string Qt::formatDateTime(datetime dateTime, variant format) |
1356 \qmlmethod string Qt::formatDateTime(datetime dateTime, variant format) |
1286 This function returns the string representation of \c dateTime, formatted according to \c format. |
1357 Returns the string representation of \c dateTime, formatted according to \c format. |
1287 |
1358 |
1288 \c format for the date/time formatting functions is be specified as follows. |
1359 \c format for the date/time formatting functions is be specified as follows. |
1289 |
1360 |
1290 These expressions may be used for the date: |
1361 These expressions may be used for the date: |
1291 |
1362 |
1357 return ctxt->throwError(QLatin1String("Qt.formatDateTime(): Invalid arguments")); |
1428 return ctxt->throwError(QLatin1String("Qt.formatDateTime(): Invalid arguments")); |
1358 |
1429 |
1359 QDateTime date = ctxt->argument(0).toDateTime(); |
1430 QDateTime date = ctxt->argument(0).toDateTime(); |
1360 Qt::DateFormat enumFormat = Qt::DefaultLocaleShortDate; |
1431 Qt::DateFormat enumFormat = Qt::DefaultLocaleShortDate; |
1361 if (argCount == 2) { |
1432 if (argCount == 2) { |
1362 if (ctxt->argument(1).isString()) { |
1433 QScriptValue formatArg = ctxt->argument(1); |
1363 QString format = ctxt->argument(1).toString(); |
1434 if (formatArg.isString()) { |
|
1435 QString format = formatArg.toString(); |
1364 return engine->newVariant(qVariantFromValue(date.toString(format))); |
1436 return engine->newVariant(qVariantFromValue(date.toString(format))); |
1365 } else if (ctxt->argument(1).isNumber()) { |
1437 } else if (formatArg.isNumber()) { |
1366 enumFormat = Qt::DateFormat(ctxt->argument(1).toUInt32()); |
1438 enumFormat = Qt::DateFormat(formatArg.toUInt32()); |
1367 } else { |
1439 } else { |
1368 return ctxt->throwError(QLatin1String("Qt.formatDateTime(): Invalid datetime format")); |
1440 return ctxt->throwError(QLatin1String("Qt.formatDateTime(): Invalid datetime format")); |
1369 } |
1441 } |
1370 } |
1442 } |
1371 return engine->newVariant(qVariantFromValue(date.toString(enumFormat))); |
1443 return engine->newVariant(qVariantFromValue(date.toString(enumFormat))); |
1372 } |
1444 } |
|
1445 #endif // QT_NO_TEXTDATE |
1373 |
1446 |
1374 /*! |
1447 /*! |
1375 \qmlmethod color Qt::rgba(real red, real green, real blue, real alpha) |
1448 \qmlmethod color Qt::rgba(real red, real green, real blue, real alpha) |
1376 |
1449 |
1377 This function returns a Color with the specified \c red, \c green, \c blue and \c alpha components. |
1450 Returns a color with the specified \c red, \c green, \c blue and \c alpha components. |
1378 All components should be in the range 0-1 inclusive. |
1451 All components should be in the range 0-1 inclusive. |
1379 */ |
1452 */ |
1380 QScriptValue QDeclarativeEnginePrivate::rgba(QScriptContext *ctxt, QScriptEngine *engine) |
1453 QScriptValue QDeclarativeEnginePrivate::rgba(QScriptContext *ctxt, QScriptEngine *engine) |
1381 { |
1454 { |
1382 int argCount = ctxt->argumentCount(); |
1455 int argCount = ctxt->argumentCount(); |