Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

Qt QML Element

The QML global Qt object provides useful enums and functions from Qt.More...

Methods

Detailed Description

TheQt object is a global object with utility functions, properties and enums.

It is not instantiable; to use it, call the members of the globalQt object directly. For example:

import QtQuick 1.0Text {color:Qt.rgba(1,0,0,1)text:Qt.md5("hello, world")}

Enums

The Qt object contains the enums available in theQt Namespace. For example, you can access theQt::LeftButton andQt::RightButton enum values asQt.LeftButton andQt.RightButton.

Types

The Qt object also contains helper functions for creating objects of specific data types. This is primarily useful when setting the properties of an item when the property has one of the following types:

  • color - use Qt.rgba(), Qt.hsla(), Qt.darker(), Qt.lighter() or Qt.tint()
  • rect - use Qt.rect()
  • point - use Qt.point()
  • size - use Qt.size()
  • vector3d - use Qt.vector3d()

There are also string based constructors for these types. SeeQML Basic Types for more information.

Date/Time Formatters

The Qt object contains several functions for formattingQDateTime,QDate andQTime values.

  • string Qt.formatDateTime(datetime date, variant format)
  • string Qt.formatDate(datetime date, variant format)
  • string Qt.formatTime(datetime date, variant format)

The format specification is described at Qt.formatDateTime.

Dynamic Object Creation

The following functions on the global object allow you to dynamically create QML items from files or strings. SeeDynamic Object Management in QML for an overview of their use.

  • object Qt.createComponent(url)
  • object Qt.createQmlObject(string qml, object parent, string filepath)

Method Documentation

stringatob(data)

ASCII to binary - this function returns a base64 decoding ofdata.


stringbtoa(data)

Binary to ASCII - this function returns a base64 encoding ofdata.


objectcreateComponent(url)

Returns aComponent object created using the QML file at the specifiedurl, ornull if an empty string was given.

The returned component'sComponent::status property indicates whether the component was successfully created. If the status isComponent.Error, seeComponent::errorString() for an error description.

CallComponent.createObject() on the returned component to create an object instance of the component.

For example:

import QtQuick 1.0Item {id:containerwidth:300;height:300functionloadButton() {        varcomponent =Qt.createComponent("Button.qml");if (component.status==Component.Ready) {            varbutton =component.createObject(container);button.color="red";        }    }Component.onCompleted:loadButton()}

SeeDynamic Object Management in QML for more information on using this function.

To create a QML object from an arbitrary string of QML (instead of a file), use Qt.createQmlObject().


objectcreateQmlObject(string qml,object parent,string filepath)

Returns a new object created from the givenstring of QML which will have the specifiedparent, ornull if there was an error in creating the object.

Iffilepath is specified, it will be used for error reporting for the created object.

Example (whereparentItem is the id of an existing QML item):

varnewObject =Qt.createQmlObject('import QtQuick 1.0; Rectangle {color: "red"; width: 20; height: 20}',parentItem,"dynamicSnippet1");

In the case of an error, aQtScript Error object is thrown. This object has an additional property,qmlErrors, which is an array of the errors encountered. Each object in this array has the memberslineNumber,columnNumber,fileName andmessage. For example, if the above snippet had misspelled color as 'colro' then the array would contain an object like the following: { "lineNumber" : 1, "columnNumber" : 32, "fileName" : "dynamicSnippet1", "message" : "Cannot assign to non-existent property "colro""}.

Note that this function returns immediately, and therefore may not work if theqml string loads new components (that is, external QML files that have not yet been loaded). If this is the case, consider using Qt.createComponent() instead.

SeeDynamic Object Management in QML for more information on using this function.


colordarker(color baseColor,real factor)

Returns a color darker thanbaseColor by thefactor provided.

If the factor is greater than 1.0, this function returns a darker color. Setting factor to 3.0 returns a color that has one-third the brightness. If the factor is less than 1.0, the return color is lighter, but we recommend using the Qt.lighter() function for this purpose. If the factor is 0 or negative, the return value is unspecified.

The function converts the current RGB color to HSV, divides the value (V) component by factor and converts the color back to RGB.

Iffactor is not supplied, returns a color 50% darker thanbaseColor (factor 2.0).


list<string>fontFamilies()

Returns a list of the font families available to the application.


stringformatDate(datetime date,variant format)

Returns a string representation ofdate, optionally formatted according toformat.

Thedate parameter may be a JavaScriptDate object, adate property, aQDate, orQDateTime value. Theformat parameter may be any of the possible format values as described for Qt.formatDateTime().

Ifformat is not specified,date is formatted usingQt.DefaultLocaleShortDate.


stringformatDateTime(datetime dateTime,variant format)

Returns a string representation ofdatetime, optionally formatted according toformat.

Thedate parameter may be a JavaScriptDate object, adate property, aQDate,QTime, orQDateTime value.

Ifformat is not provided,dateTime is formatted usingQt.DefaultLocaleShortDate. Otherwise,format should be either.

  • One of theQt::DateFormat enumeration values, such asQt.DefaultLocaleShortDate orQt.ISODate
  • A string that specifies the format of the returned string, as detailed below.

Ifformat specifies a format string, it should use the following expressions to specify the date:

ExpressionOutput
dthe day as number without a leading zero (1 to 31)
ddthe day as number with a leading zero (01 to 31)
dddthe abbreviated localized day name (e.g. 'Mon' to 'Sun'). UsesQDate::shortDayName().
ddddthe long localized day name (e.g. 'Monday' to 'Qt::Sunday'). UsesQDate::longDayName().
Mthe month as number without a leading zero (1-12)
MMthe month as number with a leading zero (01-12)
MMMthe abbreviated localized month name (e.g. 'Jan' to 'Dec'). UsesQDate::shortMonthName().
MMMMthe long localized month name (e.g. 'January' to 'December'). UsesQDate::longMonthName().
yythe year as two digit number (00-99)
yyyythe year as four digit number

In addition the following expressions can be used to specify the time:

ExpressionOutput
hthe hour without a leading zero (0 to 23 or 1 to 12 if AM/PM display)
hhthe hour with a leading zero (00 to 23 or 01 to 12 if AM/PM display)
mthe minute without a leading zero (0 to 59)
mmthe minute with a leading zero (00 to 59)
sthe second without a leading zero (0 to 59)
ssthe second with a leading zero (00 to 59)
zthe milliseconds without leading zeroes (0 to 999)
zzzthe milliseconds with leading zeroes (000 to 999)
APuse AM/PM display.AP will be replaced by either "AM" or "PM".
apuse am/pm display.ap will be replaced by either "am" or "pm".

All other input characters will be ignored. Any sequence of characters that are enclosed in single quotes will be treated as text and not be used as an expression. Two consecutive single quotes ("''") are replaced by a single quote in the output.

For example, if the following date/time value was specified:

// 21 May 2001 14:13:09var dateTime=new Date(2001,5,21,14,13,09)

ThisdateTime value could be passed toQt.formatDateTime(), Qt.formatDate() or Qt.formatTime() with theformat values below to produce the following results:

FormatResult
"dd.MM.yyyy"21.05.2001
"ddd MMMM d yy"Tue May 21 01
"hh:mm:ss.zzz"14:13:09.042
"h:m:s ap"2:13:9 pm

stringformatTime(datetime time,variant format)

Returns a string representation oftime, optionally formatted according toformat.

Thetime parameter may be a JavaScriptDate object, aQTime, orQDateTime value. Theformat parameter may be any of the possible format values as described for Qt.formatDateTime().

Ifformat is not specified,time is formatted usingQt.DefaultLocaleShortDate.


colorhsla(real hue,real saturation,real lightness,real alpha)

Returns a color with the specifiedhue,saturation,lightness andalpha components. All components should be in the range 0-1 inclusive.


objectinclude(string url,jsobject callback)

Includes another JavaScript file. This method can only be used from within JavaScript files, and not regular QML files.

This imports all functions fromurl into the current script's namespace.

Qt.include() returns an object that describes the status of the operation. The object has a single property,status, that is set to one of the following values:

SymbolValueDescription
result.OK0The include completed successfully.
result.LOADING1Data is being loaded from the network.
result.NETWORK_ERROR2A network error occurred while fetching the url.
result.EXCEPTION3A JavaScript exception occurred while executing the included code. An additionalexception property will be set in this case.

Thestatus property will be updated as the operation progresses.

If provided,callback is invoked when the operation completes. The callback is passed the same object as is returned from the Qt.include() call.


boolisQtObject(object)

Returns true ifobject is a valid reference to a Qt or QML object, otherwise false.


colorlighter(color baseColor,real factor)

Returns a color lighter thanbaseColor by thefactor provided.

If the factor is greater than 1.0, this functions returns a lighter color. Setting factor to 1.5 returns a color that is 50% brighter. If the factor is less than 1.0, the return color is darker, but we recommend using the Qt.darker() function for this purpose. If the factor is 0 or negative, the return value is unspecified.

The function converts the current RGB color to HSV, multiplies the value (V) component by factor and converts the color back to RGB.

Iffactor is not supplied, returns a color 50% lighter thanbaseColor (factor 1.5).


stringmd5(data)

Returns a hex string of the md5 hash ofdata.


boolopenUrlExternally(url target)

Attempts to open the specifiedtarget url in an external application, based on the user's desktop preferences. Returns true if it succeeds, and false otherwise.


pointpoint(int x,int y)

Returns a Point with the specifiedx andy coordinates.


quit()

This function causes theQDeclarativeEngine::quit() signal to be emitted. Within theQML Viewer, this causes the launcher application to exit; to quit a C++ application when this method is called, connect theQDeclarativeEngine::quit() signal to theQCoreApplication::quit() slot.


rectrect(int x,int y,int width,int height)

Returns arect with the top-left corner atx,y and the specifiedwidth andheight.

The returned object hasx,y,width andheight attributes with the given values.


urlresolvedUrl(url url)

Returnsurl resolved relative to the URL of the caller.


colorrgba(real red,real green,real blue,real alpha)

Returns a color with the specifiedred,green,blue andalpha components. All components should be in the range 0-1 inclusive.


size(int width,int height)

Returns a Size with the specifiedwidth andheight.


colortint(color baseColor,color tintColor)

This function allows tinting one color with another.

The tint color should usually be mostly transparent, or you will not be able to see the underlying color. The below example provides a slight red tint by having the tint color be pure red which is only 1/16th opaque.

Item {Rectangle {x:0;width:80;height:80color:"lightsteelblue"    }Rectangle {x:100;width:80;height:80color:Qt.tint("lightsteelblue","#10FF0000")    }}

Tint is most useful when a subtle change is intended to be conveyed due to some event; you can then use tinting to more effectively tune the visible color.


vector3d(real x,real y,real z)

Returns a Vector3D with the specifiedx,y andz.


© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp