diff options
Diffstat (limited to 'doc/src')
61 files changed, 2667 insertions, 382 deletions
diff --git a/doc/src/declarative/basictypes.qdoc b/doc/src/declarative/basictypes.qdoc index 6abe96f..8ab06ab 100644 --- a/doc/src/declarative/basictypes.qdoc +++ b/doc/src/declarative/basictypes.qdoc @@ -355,9 +355,11 @@ \brief A list of objects. - A list of objects. While not technically a basic type, QML also - supports lists of object types. When used from QML, the engine - automatically appends each value to the list. + A list type contains a list of objects. While not technically + a basic type, QML supports lists of object types. When used + from QML, the engine automatically appends each value to the list. + Items in the list can be accessed by index using the usual + \c listName[index] syntax. For example, the \l Item class contains a list property named children that can be used like this: @@ -366,14 +368,87 @@ Item { children: [ Item { id: child1 }, - Rectangle { id: child2 }, + Rectangle { id: child2; width: 200 }, Text { id: child3 } ] + + Component.onCompleted: { + console.log("Width of child rectangle:", children[1].width) + } } \endqml - \c child1, \c child2 and \c child3 will all be added to the children list + \c child1, \c child2 and \c child3 will be added to the children list in the order in which they appear. + List \l {Adding new properties}{properties} can be created as a + \c variant type, or as a \c list<Type> type, where \c Type is the + type of the object in the list: + + \qml + Item { + property variant values: [ 10, 20, 'abc', 'xyz' ] + + property list<Rectangle> rects: [ + Rectangle { width: 100; height: 100}, + Rectangle { width: 200; height: 200} + ] + } + \endqml + + A \c variant list can contain values of any of the \l {QML Basic Types}{basic QML types} + such as numbers, strings, etc. while a \c list<Type> list can only contain values + that match (or are derived from) the specified \c Type. + + A list property can be cleared by setting it to an empty list: + + \qml + Item { + children: [] + } + \endqml + + A list property cannot be modified in any other way. Items cannot be dynamically added to + or removed from the list through JavaScript operations; any \c push() operations on the + list only modify a \e copy of the list and not the actual list. (These current limitations + are due to restrictions on \l {Property Binding} where lists are involved.) + + To create a modifiable list, create an array object from within a \c .js JavaScript file, + or implement a custom list element in C++. Here is a QML element that modifies the list in a + JavaScript file: + + \table + \row + \o + \qml + // QML + import "script.js" as Script + + Item { + Component.onCompleted: { + Script.addItem('abc') + console.log("Added:", Script.getList()[0]) + } + } + \endqml + + \o + \code + // script.js + var myArray = new Array() + + function getList() { + return myArray + } + + function addItem(item) { + myArray.push(item) + } + \endcode + \endtable + + However, note that a JavaScript list should not be used as a QML \c property value, + as the property is not updated when the list changes. + \sa {QML Basic Types} */ diff --git a/doc/src/declarative/declarativeui.qdoc b/doc/src/declarative/declarativeui.qdoc index 28a8a70..01e1302 100644 --- a/doc/src/declarative/declarativeui.qdoc +++ b/doc/src/declarative/declarativeui.qdoc @@ -45,7 +45,7 @@ language for describing user interfaces and a language runtime. A collection of C++ APIs is used to integrate these high level features with classic Qt applications. -\section2 QML, Elements and the QtDeclarative Module +\section2 QML, Elements and the Qt Declarative Module User interfaces and their behavior are described using QML, an extension to \l{About JavaScript}{JavaScript} that lets developers and designers @@ -60,14 +60,14 @@ QObject-based type system, adds support for automatic \l{Property Binding}{property bindings} and provides \l{Network Transparency}{network transparency} at the language level. -The QtDeclarative module implements the interface between the QML language +The Qt Declarative module implements the interface between the QML language and the elements available to it. It also provides a C++ API that can be used to load and interact with QML files from within Qt applications. Qt Quick builds on \l{QML for Qt programmers}{Qt's existing strengths}. QML can be be used to incrementally extend an existing application or to build completely new applications. QML is fully -\l{Extending QML in C++}{extensible from C++} through the QtDeclarative +\l{Extending QML in C++}{extensible from C++} through the Qt Declarative Module. \section1 Getting Started @@ -77,7 +77,7 @@ Module. \o \l{Introduction to the QML language} \o \l{QML for Qt Programmers} \o \l{Getting Started Programming with QML} -\o \l{Beginning Qt Quick} +\o \l{Intro to Qt Quick} \endlist \list @@ -139,7 +139,7 @@ Module. \o \l{QML Global Object} \o \l{QML Internationalization} \o \l{QML Security} -\o \l{QtDeclarative Module} +\o \l{Qt Declarative Module} \o \l{Debugging QML} \o \l{QML Viewer} \o \l{QML Performance} diff --git a/doc/src/declarative/dynamicobjects.qdoc b/doc/src/declarative/dynamicobjects.qdoc index 3921d3e..daf2ae1 100644 --- a/doc/src/declarative/dynamicobjects.qdoc +++ b/doc/src/declarative/dynamicobjects.qdoc @@ -173,7 +173,11 @@ component. Each instance runs a NumberAnimation, and when the animation has fini Alternatively, the \c application.qml could have destroyed the created object by calling \c object.destroy(). -Notice that if a \c SelfDestroyingRect instance was created statically like this: +Note that it is safe to call destroy() on an object within that object. Objects are not destroyed the +instant destroy() is called, but are cleaned up sometime between the end of that script block and the next frame +(unless you specified a non-zero delay). + +Note also that if a \c SelfDestroyingRect instance was created statically like this: \qml Item { diff --git a/doc/src/declarative/elements.qdoc b/doc/src/declarative/elements.qdoc index 54f07a2..eaa6a82 100644 --- a/doc/src/declarative/elements.qdoc +++ b/doc/src/declarative/elements.qdoc @@ -32,7 +32,7 @@ \brief A dictionary of standard QML elements. This is a dictionary of all standard QML elements made available - in the QtDeclarative module. + in the Qt Declarative module. To see the QML elements listed by functional area, see the \l{Groups Of Related QML Elements} page. diff --git a/doc/src/declarative/extending-tutorial.qdoc b/doc/src/declarative/extending-tutorial.qdoc index 3b2fe3b..dff1d9c 100644 --- a/doc/src/declarative/extending-tutorial.qdoc +++ b/doc/src/declarative/extending-tutorial.qdoc @@ -29,7 +29,7 @@ \page qml-extending-tutorial-index.html \title Tutorial: Writing QML extensions with C++ -The QtDeclarative module provides a set of APIs for extending QML through +The Qt Declarative module provides a set of APIs for extending QML through C++ extensions. You can write extensions to add your own QML types, extend existing Qt types, or call C/C++ functions that are not accessible from ordinary QML code. @@ -65,7 +65,7 @@ For example, this could be done to implement particular data models, or provide elements with custom painting and drawing capabilities, or access system features like network programming that are not accessible through built-in QML features. -In this tutorial, we will show how to use the C++ classes in the QtDeclarative +In this tutorial, we will show how to use the C++ classes in the Qt Declarative module to extend QML. The end result will be a simple Pie Chart display implemented by several custom QML types connected together through QML features like bindings and signals, and made available to the QML runtime through a plugin. @@ -260,32 +260,28 @@ custom QML types may see unexpected behavior if bindings are not implemented. The \c PieChart type currently has a string-type property and a color-type property. It could have many other types of properties. For example, it could have an -enum-type property to store a display mode for each chart: +int-type property to store an identifier for each chart: \code // C++ class PieChart : public QDeclarativeItem { - Q_ENUMS(DisplayMode) - Q_PROPERTY(DisplayMode displayMode READ displayMode WRITE setDisplayMode) + Q_PROPERTY(int chartId READ chartId WRITE setChartId NOTIFY chartIdChanged) ... public: - enum DisplayMode { - MultiLevel, - Exploded, - ThreeDimensional - }; - - void setDisplayMode(DisplayMode mode); - DisplayMode displayMode() const; + void setChartId(int chartId); + int chartId() const; ... + + signals: + void chartIdChanged(); }; // QML PieChart { ... - displayMode: PieChart.Exploded + chartId: 100 } \endcode diff --git a/doc/src/declarative/extending.qdoc b/doc/src/declarative/extending.qdoc index 18887c7..5c1b977 100644 --- a/doc/src/declarative/extending.qdoc +++ b/doc/src/declarative/extending.qdoc @@ -647,7 +647,8 @@ language. \section1 Adding new properties -New properties can be added to an existing type. These new properties are +New properties can be added to an existing type using the \c property keyword. +These new properties are available for use within QML, and also appear as regular Qt properties on the C++ object, accessible through the regular property access mechanisms. @@ -679,8 +680,12 @@ like this: property list<Item> listOfItemsProperty \endcode +Custom types must be registered with qmlRegisterType() to be usable as a property +type. Also note that list properties cannot be modified like ordinary JavaScript +arrays; see the \l {list}{list type documentation} for details. + QML supports two methods for adding a new property to a type: a new property -definition, and a property alias. +definition, and a property alias. These are shown below. \section2 Property definitions diff --git a/doc/src/declarative/qml-intro.qdoc b/doc/src/declarative/qml-intro.qdoc index f891e01..e02ce8f 100644 --- a/doc/src/declarative/qml-intro.qdoc +++ b/doc/src/declarative/qml-intro.qdoc @@ -29,7 +29,7 @@ /*! \page qml-intro.html -\title Beginning Qt Quick +\title Intro to Qt Quick \section1 Overview diff --git a/doc/src/declarative/qmlinuse.qdoc b/doc/src/declarative/qmlinuse.qdoc new file mode 100644 index 0000000..90ce02c --- /dev/null +++ b/doc/src/declarative/qmlinuse.qdoc @@ -0,0 +1,499 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial Usage +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in a +** written agreement between you and Nokia. +** +** GNU Free Documentation License +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of this +** file. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/*! +\page qmlinuse.html +\title Using QML elements + +\raw HTML + <div class="item group"> + <div class="secondaryx"> + <div class="toc"> + <h3> + <a name="toc">QML Elements</a></h3> + <ul> + <li class="level1"><a href="#basicElements">Basic QML Elements</a></li> + <li class="level1"><a href="#visualElements">QML Visual Elements</a></li> + <li class="level1"><a href="#AnimAndTrans">QML Animation and Transition Elements</a></li> + <li class="level1"><a href="#interactElement">Basic QML Interaction Elements</a></li> + <li class="level1"><a href="#eventElements">QML Event Elements</a></li> + <li class="level1"><a href="#Position">QML Positioning Elements</a></li> + <li class="level1"><a href="#stateElement">QML State Elements</a></li> + <li class="level1"><a href="#transformElement">QML Transform Elements</a></li> + <li class="level1"><a href="#utilityElement">QML Utility Elements</a></li> + <li class="level1"><a href="#modelView">Models and View Elements</a></li> + <li class="level1"><a href="#paths">Paths</a></li> + <li class="level1"><a href="#ParticleElement">Particle Elements</a></li> + <li class="level1"><a href="#bridge">Bridge Elements</a></li> + </ul> + </div> + </div> + <div class="primary"> + <h1> + Groups Of Related QML Elements</h1> + <p> + QML Elements are grouped by their respective functionalities. Certain elements are + suited for building complex components while other elements strictly dictate appearances + and color.</p> + <div class="cols two group unclear"> + <div class="col first"> + <p> + <i>add something about elements in use in general</i></p> + </div> + <div class="col"> + <img src="images/quick_screens.png" /> + </div> + </div> + </div> + </div> + <!-- tech domains start --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="basicElements"> Basic QML Elements</a></h2> + <p> + Basic elements can be extended to form more complex elements.</p> + <b>Elements:</b> + <ul> + <li><a href="qml-item.html">Item Element</a> + - The Item is the most basic of all visual items in QML. Many visual elements inherit + properties from the Item element.</li> + <li><a href="qml-component.html">Component Element</a> + - The Component element encapsulates a QML component definition.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="visualElements">QML Visual Elements</a></h2> + <p> + Visual elements offer various interactive and graphical functionalities. Visual + elements can directly set properties that dictate appearances.</p> + <b>Elements:</b> + <ul> + <li><a href="qml-borderimage.html">BorderImage + Element</a> - The BorderImage element provides an image that can be used as a border.</li> + <li><a href="qml-gradient.html">Gradient Element</a> + - The Gradient item defines a gradient fill.</li> + <li><a href="qml-gradientstop.html">GradientStop + Element</a> - The GradientStop item defines the color at a position in a Gradient.</li> + <li><a href="qml-image.html">Image Element</a> + - The Image element displays an image from a source.</li> + <li><a href="qml-rectangle.html">Rectangle Element</a> + - The Rectangle item provides a filled rectangle.</li> + <li><a href="qml-text.html">Text Element</a> + - The Text item allows the addition of formatted text to a scene.</li> + <li><a href="qml-textedit.html">TextEdit Element</a> + - The TextEdit item displays multiple lines of editable formatted text.</li> + <li><a href="qml-textinput.html">TextInput Element</a> + - The TextInput item displays an editable line of text.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="AnimAndTrans">QML Animation and Transition Elements</a></h2> + <p> + Animation and transition elements control animation behaviors. Animations can run + in parallel or in series for different value types. + </p> + <b>Elements:</b> + <ul> + <li><a href="qml-anchoranimation.html">AnchorAnimation Element</a> - + The AnchorAnimation element animates changes in anchor values.</li> + <li><a href="qml-animation.html">Animation Element</a> - The Animation + element is the base of all QML animations.</li> + <li><a href="qml-behavior.html">Behavior Element</a> - The Behavior element allows you to specify a default animation for a property change.</li> + <li><a href="qml-coloranimation.html">ColorAnimation Element</a> - The ColorAnimation element animates changes in color values.</li> + <li><a href="qml-numberanimation.html">NumberAnimation Element</a> - The NumberAnimation element animates changes in qreal-type values.</li> + <li><a href="qml-parallelanimation.html">ParallelAnimation Element</a> - The ParallelAnimation element allows animations to be run in parallel.</li> + <li><a href="qml-parentanimation.html">ParentAnimation Element</a> - The ParentAnimation element animates changes in parent values.</li> + <li><a href="qml-pauseanimation.html">PauseAnimation Element</a> - The PauseAnimation element provides a pause during an animation.</li> + <li><a href="qml-propertyaction.html">PropertyAction Element</a> - The PropertyAction element allows immediate property changes during animation.</li> + <li><a href="qml-propertyanimation.html">PropertyAnimation Element</a> - The PropertyAnimation element animates changes in property values.</li> + <li><a href="qml-rotationanimation.html">RotationAnimation Element</a> - The RotationAnimation element animates changes in rotational values.</li> + <li><a href="qml-scriptaction.html">ScriptAction Element</a> - The ScriptAction element allows scripts to be run during an animation.</li> + <li><a href="qml-sequentialanimation.html">SequentialAnimation Element</a> - The SequentialAnimation element allows animations to be run sequentially.</li> + <li><a href="qml-smoothedanimation.html">SmoothedAnimation Element</a> - The SmoothedAnimation element allows a property to smoothly track a value.</li> + <li><a href="qml-springanimation.html">SpringAnimation Element</a> - The SpringAnimation element allows a property to track a value in a spring-like + motion.</li> + <li><a href="qml-transition.html">Transition Element</a> - The Transition element defines animated transitions that occur on state changes.</li> + <li><a href="qml-vector3danimation.html">Vector3dAnimation Element</a> - The Vector3dAnimation element animates changes in QVector3d values.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="interactElement">QML Interaction Elements</h2></a> + <p> + These elements define basic interactions such as touch movements and focus management.</p> + <b>Elements:</b> + <ul> + <li><a href="qml-flickable.html">Flickable Element</a> - The Flickable item provides a surface that can be "flicked".</li> + <li><a href="qml-flipable.html">Flipable Element</a> - The Flipable item provides a surface that can be flipped or reflected.</li> + <li><a href="qml-focuspanel.html">FocusPanel Element</a> - The FocusPanel item explicitly creates a focus panel.</li> + <li><a href="qml-focusscope.html">FocusScope Element</a> - The FocusScope object explicitly creates a focus scope for focus management.</li> + <li><a href="qml-gesturearea.html">GestureArea Element</a> - The GestureArea item enables simple gesture handling.</li> + <li><a href="qml-keynavigation.html">KeyNavigation Element</a> - The KeyNavigation attached property supports key navigation by arrow keys.</li> + <li><a href="qml-keys.html">Keys Element</a> - The Keys attached property provides key handling to Items.</li> + <li><a href="qml-mousearea.html">MouseArea Element</a> - The MouseArea item enables simple mouse handling.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="eventElements">QML Event Elements</a></h2> + <p> + Key and mouse events information are provided in these event elements.</p> + <b>Elements:</b> + <ul> + <li><a href="qml-keyevent.html">KeyEvent Element</a> - The KeyEvent + object provides information about a key event.</li> + <li><a href="qml-mouseevent.html">MouseEvent Element</a> - The MouseEvent + object provides information about a mouse event.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="Position">QML Positioning Elements</a></h2> + <p> + Using positioning elements, layouts can be defined and their children accessed through + an index.</p> + <b>Elements:</b> + <ul> + <li><a href="qml-column.html">Column Element</a> - The Column + item arranges its children vertically.</li> + <li><a href="qml-flow.html">Flow Element</a> - The Flow item + arranges its children side by side, wrapping as necessary.</li> + <li><a href="qml-grid.html">Grid Element</a> - The Grid item + positions its children in a grid.</li> + <li><a href="qml-row.html">Row Element</a> - The Row item + arranges its children horizontally.</li> + <li><a href="qml-repeater.html">Repeater Element</a> - The Repeater element allows you to repeat an Item-based component using a model.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + + <h2><a name="stateElement">QML State Elements</a></h2> + <p> + States and groups of states are formed using state elements.</p> + <b>Elements:</b> + <ul> + <li><a href="qml-anchorchanges.html">AnchorChanges Element</a> - The AnchorChanges element allows you to change the anchors of an item in a state.</li> + <li><a href="qml-parentchange.html">ParentChange Element</a> - The ParentChange element allows you to reparent an Item in a state change.</li> + <li><a href="qml-propertychanges.html">PropertyChanges Element</a> - The PropertyChanges element describes new property bindings or values for a state.</li> + <li><a href="qml-state.html">State Element</a> - The State + element defines configurations of objects and properties.</li> + <li><a href="qml-statechangescript.html">StateChangeScript Element</a> - The StateChangeScript element allows you to run a script in a state.</li> + <li><a href="qml-stategroup.html">StateGroup Element</a> - The StateGroup element provides state support for non-Item elements.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="transformElement">QML Transform Elements</a></h2> + <p> + Advanced handling of transformations is controlled in transform elements.</p> + <b>Elements:</b> + <ul> + <li><a href="qml-rotation.html">Rotation Element</a> - The Rotation object provides a way to rotate an Item.</li> + <li><a href="qml-scale.html">Scale Element</a> - The Scale element provides a way to scale an Item.</li> + <li><a href="qml-transform.html">Transform Element</a> - The Transform element provide a way of building advanced transformations on Items.</li> + <li><a href="qml-translate.html">Translate Element</a> - The Translate object provides a way to move an Item without changing its x or y properties.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="utilityElement">QML Utility Elements</a></h2> + <p> + These elements handle assorted operations such as event timing, Qt enumerations, + and font loading.</p> + <b>Elements:</b> + <ul> + <li><a href="qml-binding.html">Binding Element</a> - The Binding element allows arbitrary property bindings to be created.</li> + <li><a href="qml-connections.html">Connections Element</a> - A Connections element describes generalized connections to signals.</li> + <li><a href="qml-doublevalidator.html">DoubleValidator Element</a> - Provides a validator for non-integer numbers.</li> + <li><a href="qml-fontloader.html">FontLoader Element</a> - The FontLoader element allows fonts to be loaded by name or URL.</li> + <li><a href="qml-intvalidator.html">IntValidator Element</a> - This element provides a validator for integer values.</li> + <li><a href="qml-layoutitem.html">LayoutItem Element</a> - The LayoutItem element allows declarative UI elements to be placed inside Qt's Graphics View layouts.</li> + <li><a href="qml-loader.html">Loader Element</a> - The Loader item allows dynamically loading an Item-based subtree from a URL or Component.</li> + <li><a href="qml-package.html">Package Element</a> - Package provides a bundle for shared contexts in multiple views.</li> + <li><a href="qml-qt.html">Qt Element</a> - The QML global Qt object provides useful enums and functions from Qt.</li> + <li><a href="qml-qtobject.html">QtObject Element</a> - The QtObject element is the most basic element in QML.</li> + <li><a href="qml-regexpvalidator.html">RegExpValidator Element</a> - This element provides a validator for regular expressions.</li> + <li><a href="qml-systempalette.html">SystemPalette Element</a> - The SystemPalette element provides access to the Qt palettes.</li> + <li><a href="qml-timer.html">Timer Element</a> - The Timer item triggers a handler at a specified interval.</li> + <li><a href="qml-workerscript.html">WorkerScript Element</a> - The WorkerScript element enables the use of threads in QML.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="modelView">Models and View Elements</a></h2> + <p> + Models and views are used to organize data and control their layouts using delegates. + Models dictate the data formation and views control the layouts of data in the model.</p> + <b>View Elements:</b> + <ul> + <li><a href="qml-gridview.html">GridView Element</a> - The GridView item provides a grid view of items provided by a model.</li> + <li><a href="qml-listview.html">ListView Element</a> - The ListView item provides a list view of items provided by a model.</li> + <li><a href="qml-pathview.html">PathView Element</a> - The PathView element lays out model-provided items on a path.</li> + <li><a href="qml-webview.html">WebView Element</a> - The WebView item allows you to add Web content to a canvas.</li> + </ul> + <b>Model Elements:</b> + <ul> + <li><a href="qml-folderlistmodel.html">FolderListModel Element</a> - The FolderListModel provides a model of the contents of a file system folder.</li> + <li><a href="qml-listelement.html">ListElement Element</a> - A ListElement defines a data item in a ListModel.</li> + <li><a href="qml-listmodel.html">ListModel Element</a> - The ListModel element defines a free-form list data source.</li> + <li><a href="qml-visualdatamodel.html">VisualDataModel Element</a> - The VisualDataModel encapsulates a model and delegate.</li> + <li><a href="qml-visualitemmodel.html">VisualItemModel Element</a> - The VisualItemModel allows items to be provided to a view.</li> + <li><a href="qml-xmllistmodel.html">XmlListModel Element</a> - The XmlListModel element is used to specify a model using XPath expressions.</li> + <li><a href="qml-xmlrole.html">XmlRole Element</a> - The XmlRole element allows you to specify a role for an XmlListModel.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="paths">Paths</a></h2> + <p> + QML components can be arranged along paths. Path elements allow control over different + path types.</p> + <b>Elements:</b> + <ul> + <li><a href="qml-path.html">Path Element</a> - A Path object defines a path for use by PathView.</li> + <li><a href="qml-pathattribute.html">PathAttribute Element</a> - The PathAttribute allows setting an attribute at a given position in a Path.</li> + <li><a href="qml-pathcubic.html">PathCubic Element</a> - The PathCubic defines a cubic Bezier curve with two control points.</li> + <li><a href="qml-pathelement.html">PathElement Element</a> - PathElement is the base path type.</li> + <li><a href="qml-pathline.html">PathLine Element</a> - The PathLine defines a straight line.</li> + <li><a href="qml-pathpercent.html">PathPercent Element</a> - The PathPercent manipulates the way a path is interpreted.</li> + <li><a href="qml-pathquad.html">PathQuad Element</a> - The PathQuad defines a quadratic Bezier curve with a control point.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="ParticleElement">Particle Elements</a></h2> + <p> + Particle effects are declared and controlled using particle elements.</p> + <b>Elements:</b> + <ul> + <li><a href="qml-particlemotiongravity.html">ParticleMotionGravity Element</a> - The ParticleMotionGravity object moves particles towards a point.</li> + <li><a href="qml-particlemotionlinear.html">ParticleMotionLinear Element</a> - The ParticleMotionLinear object moves particles linearly.</li> + <li><a href="qml-particlemotionwander.html">ParticleMotionWander Element</a> - The ParticleMotionWander object moves particles in a somewhat random fashion.</li> + <li><a href="qml-particles.html">Particles Element</a> - The Particles object generates and moves particles.</li> + </ul> + </div> + </div> + <!-- next --> + <div class="item group"> + <hr> + <div class="secondary"> + <div class="box"> + <!-- video box --> + <h3> + image heading</h3> + <img src="" /> + <p> + img descr.</p> + </div> + <!-- video box end --> + </div> + <div class="primary"> + <h2><a name="bridge">Bridge Elements</a></h2> + <p> + Bridge elements allow direct communication between C++ and QML entities.</p> + <b>Elements:</b> + <ul> + <li><a href="qml-layoutitem.html">LayoutItem Element</a> - The LayoutItem element allows declarative UI elements to be placed inside Qt's Graphics View layouts.</li> + </ul> + </div> + </div> + +\endraw + + + +*/ + diff --git a/doc/src/declarative/qtbinding.qdoc b/doc/src/declarative/qtbinding.qdoc index 58d53de..cd50503 100644 --- a/doc/src/declarative/qtbinding.qdoc +++ b/doc/src/declarative/qtbinding.qdoc @@ -30,126 +30,389 @@ \target qtbinding \title Using QML in C++ Applications -\tableofcontents +QML is designed to be easily extensible from C++. The classes in the +Qt Declarative module allow QML components to be loaded and manipulated from C++, and through +Qt's \l{The Meta-Object System}{meta-object system}, QML and C++ objects can easily +communicate through Qt signals and slots. In addition, QML plugins can be written to create +reusable QML components for distribution. -The QML API is split into three main classes - QDeclarativeEngine, QDeclarativeComponent and QDeclarativeContext. -QDeclarativeEngine provides the environment in which QML is run, QDeclarativeComponent encapsulates -\l {QML Documents}, and QDeclarativeContext allows applications to expose data to QML component instances. +You may want to mix QML and C++ for a number of reasons. For example: -QML also includes a convenience API, QDeclarativeView, for applications that simply want to embed QML -components into a new QGraphicsView. QDeclarativeView covers up many of the details discussed below. -While QDeclarativeView is mainly intended for rapid prototyping it can have uses in production applications. +\list +\o To use functionality defined in a C++ source (for example, when using a C++ Qt-based data model, or +calling functions in a third-party C++ library) +\o To access functionality in the Qt Declarative module (for example, to dynamically generate +images using QDeclarativeImageProvider) +\o To write your own QML elements (whether for your applications, or for distribution to others) +\endlist + +To use the Qt Declarative module, you must include and link to the module appropriately, as shown on +the \l {QtDeclarative}{module index page}. The \l {Qt Declarative UI Runtime} documentation +shows how to build a basic C++ application that uses this module. + + +\section1 Core module classes -If you are looking at retrofitting an existing Qt application with QML, -read \l{Integrating QML with existing Qt UI code}. -\section1 Basic Usage +The Qt Declarative module provides a set of C++ APIs for extending your QML applications from C++ and +embedding QML into C++ applications. There are several core classes in the Qt Declarative module +that provide the essential capabilities for doing this. These are: -Every application requires at least one QDeclarativeEngine. A QDeclarativeEngine allows the configuration of -global settings that apply to all the QML component instances - such as the QNetworkAccessManager -that is used for network communications, and the path used for persistent storage. -Multiple QDeclarativeEngine's are only needed if the application requires these settings to differ -between QML component instances. +\list +\o QDeclarativeEngine: A QML engine provides the environment for executing QML code. Every +application requires at least one engine instance. +\o QDeclarativeComponent: A component encapsulates a \l{QML Documents}{QML document}. +\o QDeclarativeContext: A context allows an application to expose data to the QML components +created by an engine. +\endlist -\l {QML Documents} are loaded using the QDeclarativeComponent class. Each QDeclarativeComponent instance -represents a single QML document. A QDeclarativeComponent can be passed a document URL, or raw text -representing the content of the document. The document URL can be a local filesystem URL, or -any network URL supported by QNetworkAccessManager. +A QDeclarativeEngine allows the configuration of global settings that apply to all of its QML +component instances: for example, the QNetworkAccessManager to be used for network communications, +and the file path to be used for persistent storage. -QML component instances can then be created by calling the QDeclarativeComponent::create() method. Here's -an example of loading a QML document, and creating an object from it. +QDeclarativeComponent is used to load QML documents. Each QDeclarativeComponent instance represents +a single document. A component can be created from the URL or file path of a QML document, or the raw +QML code of the document. Component instances are instatiated through the +QDeclarativeComponent::create() method, like this: \code - QDeclarativeEngine *engine = new QDeclarativeEngine(parent); - QDeclarativeComponent component(engine, QUrl::fromLocalFile("main.qml")); - QObject *myObject = component.create(); +QDeclarativeEngine engine; +QDeclarativeComponent component(&engine, QUrl::fromLocalFile("MyRectangle.qml")); +QObject *rectangleInstance = component.create(); + +// ... +delete rectangleInstance; \endcode -\section1 Exposing Data +QML documents can also be loaded using QDeclarativeView. This class provides a convenient +QWidget-based view for embedding QML components into QGraphicsView-based applications. (For other +methods of integrating QML into QWidget-based applications, see \l {Integrating QML with existing Qt +UI code}.) + + +\section1 Approaches to using QML with C++ + +There are a number of ways to extend your QML application through C++. For example, you could: + +\list +\o Load a QML component and manipulate it (or its children) from C++ +\o Embed a C++ object and its properties directly into a QML component (for example, to make a +particular C++ object callable from QML, or to replace a dummy list model data with a real data set) +\o Define new QML elements (through QObject-based C++ classes) and create them directly from your +QML code +\endlist + +These methods are shown below. Naturally these approaches are not exclusive; you can mix any of +these methods throughout your application as appropriate. + + +\section2 Loading QML components from C++ + +A QML document can be loaded with QDeclarativeComponent or QDeclarativeView. QDeclarativeComponent +loads a QML component as a C++ object; QDeclarativeView also does this, +but additionally loads the QML component directly into a QGraphicsView. It is convenient for loading +a displayable QML component into a QWidget-based application. + +For example, suppose there is a \c MyItem.qml file that looks like this: + +\snippet doc/src/snippets/declarative/qtbinding/loading/MyItem.qml start +\snippet doc/src/snippets/declarative/qtbinding/loading/MyItem.qml end + +This QML document can be loaded with QDeclarativeComponent or QDeclarativeView with the following +C++ code. Using a QDeclarativeComponent requires calling QDeclarativeComponent::create() to create +a new instance of the component, while a QDeclarativeView automatically creates an instance of the +component, which is accessible via QDeclarativeView::rootObject(): + +\table +\row +\o +\snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp QDeclarativeComponent-a +\dots 0 +\snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp QDeclarativeComponent-b +\o +\snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp QDeclarativeView +\endtable + +This \c object is the instance of the \c MyItem.qml component that has been created. You can now +modify the item's properties using QObject::setProperty() or QDeclarativeProperty: + +\snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp properties + +Alternatively, you can cast the object to its actual type and call functions with compile-time +safety. In this case the base object of \c MyItem.qml is an \l Item, which is defined by the +QDeclarativeItem class: + +\snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp cast + +You can also connect to any signals or call functions defined in the component using +QMetaObject::invokeMethod() and QObject::connect(). See \l {Exchanging data between QML and C++} +below for further details. + +\section3 Locating child objects + +QML components are essentially object trees with children that have siblings and their own children. +Child objects of QML components can be located using the QObject::objectName property with +QObject::findChild(). For example, if the root item in \c MyItem.qml had a child \l Rectangle item: + +\snippet doc/src/snippets/declarative/qtbinding/loading/MyItem.qml start +\codeline +\snippet doc/src/snippets/declarative/qtbinding/loading/MyItem.qml child +\snippet doc/src/snippets/declarative/qtbinding/loading/MyItem.qml end + +The child could be located like this: + +\snippet doc/src/snippets/declarative/qtbinding/loading/main.cpp findChild + +If \c objectName is used inside a delegate of a ListView, \l Repeater or some other +element that creates multiple instances of its delegates, there will be multiple children with +the same \c objectName. In this case, QObject::findChildren() can be used to find all children +with a matching \c objectName. + +\warning While it is possible to use C++ to access and manipulate QML objects deep into the +object tree, we recommend that you do not take this approach outside of application +testing and prototyping. One strength of QML and C++ integration is the ability to implement the +QML user interface separately from the C++ logic and dataset backend, and this strategy breaks if the +C++ side reaches deep into the QML components to manipulate them directly. This would make it difficult +to, for example, swap a QML view component for another view, if the new component was missing a +required \c objectName. It is better for the C++ implementation to know as little as possible about +the QML user interface implementation and the composition of the QML object tree. + + +\section2 Embedding C++ objects into QML components + +When loading a QML scene into a C++ application, it can be useful to directly embed C++ data into +the QML object. QDeclarativeContext enables this by exposing data to the context of a QML +component, allowing data to be injected from C++ into QML. + +For example, here is a QML item that refers to a \c currentDateTime value that does not exist in +the current scope: + +\snippet doc/src/snippets/declarative/qtbinding/context/MyItem.qml 0 + +This \c currentDateTime value can be set directly by the C++ application that loads the QML +component, using QDeclarativeContext::setContextProperty(): + +\snippet doc/src/snippets/declarative/qtbinding/context/main.cpp 0 + +Context properties can hold either QVariant or QObject* values. This means custom C++ objects can +also be injected using this approach, and these objects can be modified and read directly in QML. +Here, we modify the above example to embed a QObject instance instead of a QDateTime value, and the QML code +invokes a method on the object instance: + +\table +\row +\o +\snippet doc/src/snippets/declarative/qtbinding/context-advanced/applicationdata.h 0 +\codeline +\snippet doc/src/snippets/declarative/qtbinding/context-advanced/main.cpp 0 +\o +\snippet doc/src/snippets/declarative/qtbinding/context-advanced/MyItem.qml 0 +\endtable + +(Note that date/time values returned from C++ to QML can be formatted through +\l{QML:Qt::formatDateTime}{Qt.formatDateTime()} and associated functions.) + +If the QML item needs to receive signals from the context property, it can connect to them using the +\l Connections element. For example, if \c ApplicationData has a signal named \c +dataChanged(), this signal can be connected to using an \c onDataChanged handler within +a \l Connections object: + +\snippet doc/src/snippets/declarative/qtbinding/context-advanced/connections.qml 0 + +Context properties can be useful for using C++ based data models in a QML view. See the +\l {declarative/modelviews/stringlistmodel}{String ListModel}, +\l {declarative/modelviews/objectlistmodel}{Object ListModel} and +\l {declarative/modelviews/abstractitemmodel}{AbstractItemModel} models for +respective examples on using QStringListModel, QObjectList-based models and QAbstractItemModel +in QML views. + +Also see the QDeclarativeContext documentation for more information. + + +\section2 Defining new QML elements + +While new QML elements can be \l {Defining new Components}{defined in QML}, they can also be +defined by C++ classes; in fact, many of the core \l {QML Elements} are implemented through +C++ classes. When you create a QML object using one of these elements, you are simply creating an +instance of a QObject-based C++ class and setting its properties. + +For example, here is an \c ImageViewer class with an \c image URL property: + +\snippet doc/src/snippets/declarative/qtbinding/newelements/imageviewer.h 0 + +Aside from the fact that it inherits QDeclarativeItem, this is an ordinary class that could +exist outside of QML. However, once it is registered with the QML engine using qmlRegisterType(): + +\snippet doc/src/snippets/declarative/qtbinding/newelements/main.cpp register + +Then, any QML code loaded by your C++ application or \l{QDeclarativeExtensionPlugin}{plugin} can create and manipulate +\c ImageViewer objects: + +\snippet doc/src/snippets/declarative/qtbinding/newelements/standalone.qml 0 + +Note that custom C++ types do not have to inherit from QDeclarativeItem; this is only necessary if it is +a displayable item. If the item is not displayable, it can simply inherit from QObject. + +For more information on defining new QML elements, see the \l {Tutorial: Writing QML extensions with C++} +{Writing QML extensions with C++} tutorial and the \l {Extending QML in C++} reference +documentation. + + + +\section1 Exchanging data between QML and C++ + +QML and C++ objects can communicate with one another through signals, slots and property +modifications. For a C++ object, any data that is exposed to Qt's \l{The Meta-Object System}{Meta-Object System} +- that is, properties, signals, slots and Q_INVOKABLE methods - become available to QML. On +the QML side, all QML object data is automatically made available to the meta-object system and can +be accessed from C++. + + +\section2 Calling functions -QML components are instantiated in a QDeclarativeContext. A context allows the application to expose data -to the QML component instance. A single QDeclarativeContext can be used to instantiate all the objects -used by an application, or several QDeclarativeContext can be created for more fine grained control over -the data exposed to each instance. If a context is not passed to the QDeclarativeComponent::create() -method, the QDeclarativeEngine's \l {QDeclarativeEngine::rootContext()}{root context} is used. Data exposed through -the root context is available to all object instances. +QML functions can be called from C++ and vice-versa. -\section1 Simple Data +All QML functions are exposed to the meta-object system and can be called using +QMetaObject::invokeMethod(). Here is a C++ application that uses this to call a QML function: -To expose data to a QML component instance, applications set \l {QDeclarativeContext::setContextProperty()} -{context properties} which are then accessible by name from QML \l {Property Binding}s and JavaScript. -The following example shows how to expose a background color to a QML file through QDeclarativeView: +\table +\row +\o \snippet doc/src/snippets/declarative/qtbinding/functions-qml/MyItem.qml 0 +\o \snippet doc/src/snippets/declarative/qtbinding/functions-qml/main.cpp 0 +\endtable + +Notice the Q_RETURN_ARG() and Q_ARG() arguments for QMetaObject::invokeMethod() must be specified as +QVariant types, as this is the generic data type used for QML functions and return values. + +To call a C++ function from QML, the function must be either a Qt slot, or a function marked with +the Q_INVOKABLE macro, to be available to QML. In the following example, the QML code invokes +methods on the \c myObject object, which has been set using QDeclarativeContext::setContextProperty(): + +\table +\row +\o +\snippet doc/src/snippets/declarative/qtbinding/functions-cpp/MyItem.qml 0 +\o +\snippet doc/src/snippets/declarative/qtbinding/functions-cpp/myclass.h 0 +\codeline +\snippet doc/src/snippets/declarative/qtbinding/functions-cpp/main.cpp 0 +\endtable + +Note that QML does not support overloaded functions. If a C++ has more than one function with the +same name, there is no guarantee which overloaded function will be called from QML. + + +\section2 Receiving signals + +All QML signals are automatically available to C++, and can be connected to using QObject::connect() +like any ordinary Qt C++ signal. + +Here is a QML component with a signal named \c qmlSignal. This signal is connected to a C++ object's +slot using QObject::connect(): + +\table +\row +\o +\snippet doc/src/snippets/declarative/qtbinding/signals-qml/MyItem.qml 0 +\o +\snippet doc/src/snippets/declarative/qtbinding/signals-qml/myclass.h 0 +\codeline +\snippet doc/src/snippets/declarative/qtbinding/signals-qml/main.cpp 0 +\endtable + +To connect to Qt C++ signals from within QML, use a signal handler with the \c on<SignalName> syntax. +If the C++ object is directly creatable from within QML (see \l {Defining new QML elements} above) +then the signal handler can be defined within the object declaration. In the following example, the +QML code creates a \c ImageViewer object, and the \c imageChanged and \c loadingError signals of the +C++ object are connected to through \c onImagedChanged and \c onLoadingError signal handlers in QML: \table \row \o -\c {// main.cpp} -\snippet doc/src/snippets/declarative/qtbinding/contextproperties/main.cpp 0 + +\snippet doc/src/snippets/declarative/qtbinding/signals-cpp/imageviewer.h start +\dots 4 +\snippet doc/src/snippets/declarative/qtbinding/signals-cpp/imageviewer.h end \o -\c {// main.qml} -\snippet doc/src/snippets/declarative/qtbinding/contextproperties/main.qml 0 +\snippet doc/src/snippets/declarative/qtbinding/signals-cpp/standalone.qml 0 +\endtable + +(Note that if a signal has been declared as the NOTIFY signal for a property, QML allows it to be +received with an \c on<Property>Changed handler even if the signal's name does not follow the \c +<Property>Changed naming convention. In the above example, if the "imageChanged" signal was named +"imageModified" instead, the \c onImageChanged signal handler would still be called.) + +If, however, the object with the signal is not created from within the QML code, and the QML item only has a +reference to the created object - for example, if the object was set using +QDeclarativeContext::setContextProperty() - then the \l Connections element can be used +instead to create the signal handler: +\table +\row +\o \snippet doc/src/snippets/declarative/qtbinding/signals-cpp/main.cpp connections +\o \snippet doc/src/snippets/declarative/qtbinding/signals-cpp/MyItem.qml 0 \endtable -Or, if you want \c main.cpp to create the component without showing it in a QDeclarativeView, you could create an instance of QDeclarativeContext using QDeclarativeEngine::rootContext() instead: -\snippet doc/src/snippets/declarative/qtbinding/contextproperties/main.cpp 1 +\section2 Modifying properties -Context properties work just like normal properties in QML bindings - if the \c backgroundColor -context property in this example was changed to red, the component object instances would -all be automatically updated. Note that it is the responsibility of the creator to delete any -QDeclarativeContext it constructs. If the \c windowContext is no longer needed when -the \c window component instantiation is destroyed, the \c windowContext must be destroyed -explicitly. The simplest way to ensure this is to set \c window as \c windowContext's parent. +Any properties declared in a QML object are automatically accessible from C++. Given a QML item +like this: -QDeclarativeContexts form a tree - each QDeclarativeContext except for the root context has a parent. Child -QDeclarativeContexts effectively inherit the context properties present in their parents. This gives -applications more freedom in partitioning the data exposed to different QML object instances. -If a QDeclarativeContext sets a context property that is also set in one of its parents, the new context -property shadows that in the parent. In The following example, the \c background context property -in \c {Context 1} shadows the \c background context property in the root context. +\snippet doc/src/snippets/declarative/qtbinding/properties-qml/MyItem.qml 0 -\image qml-context-tree.png +The value of the \c someNumber property can be set and read using QDeclarativeProperty, or +QObject::setProperty() and QObject::property(): -\section2 Structured Data +\snippet doc/src/snippets/declarative/qtbinding/properties-qml/main.cpp 0 -Context properties can also be used to expose structured and writable data to QML objects. In -addition to all the types already supported by QVariant, QObject derived types can be assigned to -context properties. QObject context properties allow the data exposed to be more structured, and -allow QML to set values. +You should always use QObject::setProperty(), QDeclarativeProperty or QMetaProperty::write() to +change a QML property value, to ensure the QML engine is made aware of the property change. For example, +say you have a custom element \c PushButton with a \c buttonText property that internally reflects +the value of a \c m_buttonText member variable. Modifying the member variable directly like this is +not a good idea: -The following example creates a \c CustomPalette object, and sets it as the \c palette context -property. +\badcode +// BAD! +QDeclarativeComponent component(engine, "MyButton.qml"); +PushButton *button = qobject_cast<PushButton*>(component.create()); +button->m_buttonText = "Click me"; +\endcode -\snippet doc/src/snippets/declarative/qtbinding/custompalette/custompalette.h 0 +Since the value is changed directly, this bypasses Qt's \l{The Meta-Object System}{meta-object system} +and the QML engine is not made aware of the property change. This means property bindings to +\c buttonText would not be updated, and any \c onButtonTextChanged handlers would not be called. -\snippet doc/src/snippets/declarative/qtbinding/custompalette/main.cpp 0 -The QML that follows references the palette object, and its properties, to set the appropriate -background and text colors. When the window is clicked, the palette's text color is changed, and -the window text will update accordingly. +\target properties-cpp -\snippet doc/src/snippets/declarative/qtbinding/custompalette/main.qml 0 +Any \l {The Property System}{Qt properties} - that is, those declared with the Q_PROPERTY() +macro - are accessible from QML. Here is a modified version of the \l {Embedding C++ objects into +QML components}{earlier example} on this page; here, the \c ApplicationData class has a \c backgroundColor +property. This property can be written to and read from QML: -To detect when a C++ property value - in this case the \c CustomPalette's \c text property - -changes, the property must have a corresponding NOTIFY signal. The NOTIFY signal specifies a signal -that is emitted whenever the property changes value. Implementers should take care to only emit the -signal if the value \e changes to prevent loops from occurring. Accessing a property from a -binding that does not have a NOTIFY signal will cause QML to issue a warning at runtime. +\table +\row +\o \snippet doc/src/snippets/declarative/qtbinding/properties-cpp/applicationdata.h 0 +\o \snippet doc/src/snippets/declarative/qtbinding/properties-cpp/MyItem.qml 0 +\endtable -\section2 Dynamic Structured Data +Notice the \c backgroundColorChanged signal is declared as the NOTIFY signal for the +\c backgroundColor property. If a Qt property does not have an associated NOTIFY signal, +the property cannot be used for \l {Property Binding} in QML, as the QML engine would not be +notified when the value changes. If you are using custom types in QML, make sure their +properties have NOTIFY signals so that they can be used in property bindings. -If an application is too dynamic to structure data as compile-time QObject types, dynamically -structured data can be constructed at runtime using the QDeclarativePropertyMap class. +See \l {Tutorial: Writing QML extensions with C++} for further details and examples +on using Qt properties with QML. -\section1 Calling C++ methods from QML +\section1 Supported data types -It is possible to call methods of QObject derived types by either exposing the -methods as public slots, or by marking the methods Q_INVOKABLE. +Any C++ data that is used from QML - whether as custom properties, or parameters for signals or +functions - must be of a type that is recognizable by QML. -The C++ methods can also have parameters and return values. QML has support for -the following types: +By default, QML recognizes the following data types: \list \o bool @@ -163,102 +426,168 @@ the following types: \o QSize, QSizeF \o QRect, QRectF \o QVariant +\o QObject* +\o Enumerations declared with Q_ENUMS() \endlist -This example toggles the "Stopwatch" object on/off when the MouseArea is clicked: +To allow a custom C++ type to be created or used in QML, the C++ class must be registered as a QML +type using qmlRegisterType(), as shown in the \l {Defining new QML elements} section above. -\table -\row -\o -\c {// main.cpp} -\snippet doc/src/snippets/declarative/qtbinding/stopwatch/stopwatch.h 0 -\snippet doc/src/snippets/declarative/qtbinding/stopwatch/main.cpp 0 -\o -\c {// main.qml} -\snippet doc/src/snippets/declarative/qtbinding/stopwatch/main.qml 0 +\section2 Using enumerations of a custom type -\endtable +To use an enumeration from a custom C++ component, the enumeration must be declared with Q_ENUMS() to +register it with Qt's meta object system. For example, the following C++ type has a \c Status enum: -Note that in this particular example a better way to achieve the same result -is to have a "running" property in \c main.qml. This leads to much nicer QML code: +\snippet doc/src/snippets/declarative/qtbinding/enums/imageviewer.h start +\snippet doc/src/snippets/declarative/qtbinding/enums/imageviewer.h end + +Providing the \c ImageViewer class has been registered using qmlRegisterType(), its \c Status enum can +now be used from QML: + +\snippet doc/src/snippets/declarative/qtbinding/enums/standalone.qml 0 + +The C++ type must be registered with QML to use its enums. If your C++ type is not instantiable, it +can be registered using qmlRegisterUncreatableType(). + +See the \l {Tutorial: Writing QML extensions with C++}{Writing QML extensions with C++} tutorial and +the \l {Extending QML in C++} reference documentation for more information. + + +\section2 Automatic type conversion + +As a convenience, some basic types can be specified in QML using format strings to make it easier to +pass simple values from QML to C++. \table +\header +\o Type +\o String format +\o Example \row -\o -\code -// main.qml -import QtQuick 1.0 - -Rectangle { - MouseArea { - anchors.fill: parent - onClicked: stopwatch.running = !stopwatch.running - } -} -\endcode +\o QColor +\o Color name, "#RRGGBB", "#RRGGBBAA" +\o "red", "#ff0000", "#ff000000" +\row +\o QDate +\o "YYYY-MM-DD" +\o "2010-05-31" +\row +\o QPoint +\o "x,y" +\o "10,20" +\row +\o QRect +\o "x,y,WidthxHeight" +\o "50,50,100x100" +\row +\o QSize +\o "WidthxHeight" +\o "100x200" +\row +\o QTime +\o "hh:mm:ss" +\o "14:22:55" +\row +\o QUrl +\o URL string +\o "http://www.example.com" +\row +\o QVector3D +\o "x,y,z" +\o "0,1,0" +\row +\o Enumeration value +\o Enum value name +\o "AlignRight" \endtable -Of course, it is also possible to call \l {Adding new methods}{functions declared in QML from C++}. +(More details on these string formats and types can be found in the +\l {QML Basic Types}{basic type documentation}.) + +These string formats can be used to set QML \c property values and pass arguments to C++ +functions. This is demonstrated by various examples on this page; in the above +\l{#properties-cpp}{Qt properties example}, the \c ApplicationData class has a \c backgroundColor +property of a QColor type, which is set from the QML code with the string "red" rather rather +than an actual QColor object. + +If it is preferred to pass an explicitly-typed value rather than a string, the global +\l{QmlGlobalQtObject}{Qt object} provides convenience functions for creating some of the object +types listed above. For example, \l{QML:Qt::rgba()}{Qt.rgba()} creates a QColor value from four +RGBA values. The QColor returned from this function could be used instead of a string to set +a QColor-type property or to call a C++ function that requires a QColor parameter. + + +\section1 Writing QML plugins + +The Qt Declarative module includes the QDeclarativeExtensionPlugin class, which is an abstract +class for writing QML plugins. This allows QML extension types to be dynamically loaded into +QML applications. + +See the QDeclarativeExtensionPlugin documentation and \l {How to Create Qt Plugins} for more +details. + + +\section1 Managing resource files with the Qt resource system +The \l {The Qt Resource System}{Qt resource system} allows resource files to be stored as +binary files in an application executable. This can be useful when building a mixed +QML/C++ application as it enables QML files (as well as other resources such as images +and sound files) to be referred to through the resource system URI scheme rather than +relative or absolute paths to filesystem resources. Note, however, that if you use the resource +system, the application executable must be re-compiled whenever a QML source file is changed +in order to update the resources in the package. -\section1 Network Components +To use the resource system in a mixed QML/C++ application: -If the URL passed to QDeclarativeComponent is a network resource, or if the QML document references a -network resource, the QDeclarativeComponent has to fetch the network data before it is able to create -objects. In this case, the QDeclarativeComponent will have a \l {QDeclarativeComponent::Loading}{Loading} -\l {QDeclarativeComponent::status()}{status}. An application will have to wait until the component -is \l {QDeclarativeComponent::Ready}{Ready} before calling \l {QDeclarativeComponent::create()}. +\list +\o Create a \c .qrc \l {The Qt Resource System}{resource collection file} that lists resource + files in XML format +\o From C++, load the main QML file as a resource using the \c :/ prefix or as a URL with the + \c qrc scheme +\endlist + +Once this is done, all files specified by relative paths in QML will be loaded from +the resource system instead. Use of the resource system is completely transparent to +the QML layer; this means all QML code should refer to resource files using relative +paths and should \e not use the \c qrc scheme. This scheme should only be used from +C++ code for referring to resource files. -The following example shows how to load a QML file from a network resource. After creating -the QDeclarativeComponent, it tests whether the component is loading. If it is, it connects to the -QDeclarativeComponent::statusChanged() signal and otherwise calls the \c {continueLoading()} method -directly. This test is necessary, even for URLs that are known to be remote, just in case -the component has been cached and is ready immediately. +Here is a application packaged using the \l {The Qt Resource System}{Qt resource system}. +The directory structure looks like this: \code -MyApplication::MyApplication() -{ - // ... - component = new QDeclarativeComponent(engine, QUrl("http://www.example.com/main.qml")); - if (component->isLoading()) - QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), - this, SLOT(continueLoading())); - else - continueLoading(); -} - -void MyApplication::continueLoading() -{ - if (component->isError()) { - qWarning() << component->errors(); - } else { - QObject *myObject = component->create(); - } -} +project + |- example.qrc + |- main.qml + |- images + |- background.png + |- main.cpp + |- project.pro \endcode -\section1 Qt Resources +The \c main.qml and \c background.png files will be packaged as resource files. This is +done in the \c example.qrc resource collection file: -QML content can be loaded from \l {The Qt Resource System} using the \e qrc: URL scheme. -For example: +\quotefile doc/src/snippets/declarative/qtbinding/resources/example.qrc -\c [project/example.qrc] -\quotefile doc/src/snippets/declarative/qtbinding/resources/example.qrc +Since \c background.png is a resource file, \c main.qml can refer to it using the relative +path specified in \c example.qrc: + +\snippet doc/src/snippets/declarative/qtbinding/resources/main.qml 0 -\c [project/project.pro] -\quotefile doc/src/snippets/declarative/qtbinding/resources/resources.pro +To allow QML to locate resource files correctly, the \c main.cpp loads the main QML +file, \c main.qml, as a resource file using the \c qrc scheme: -\c [project/main.cpp] \snippet doc/src/snippets/declarative/qtbinding/resources/main.cpp 0 -\c [project/main.qml] -\snippet doc/src/snippets/declarative/qtbinding/resources/main.qml 0 +Finally \c project.pro uses the RESOURCES variable to indicate that \c example.qrc should +be used to build the application resources: + +\quotefile doc/src/snippets/declarative/qtbinding/resources/resources.pro + +See \l {The Qt Resource System} for more information. -Note that the resource system cannot be accessed from QML directly. If the main QML file is -loaded as a resource, all files specified as relative paths in QML will also be loaded from -the resource system. Using the resource system is completely transparent to the QML layer. -This also means that if the main QML file is not loaded as a resource then files in the resource -system cannot be accessed from QML. */ + diff --git a/doc/src/declarative/qtdeclarative.qdoc b/doc/src/declarative/qtdeclarative.qdoc index 7ecdc53..f2b2032 100644 --- a/doc/src/declarative/qtdeclarative.qdoc +++ b/doc/src/declarative/qtdeclarative.qdoc @@ -27,7 +27,7 @@ /*! \module QtDeclarative - \title QtDeclarative Module + \title Qt Declarative Module \ingroup modules \brief The Qt Declarative module provides a declarative framework diff --git a/doc/src/getting-started/gettingstartedqml.qdoc b/doc/src/getting-started/gettingstartedqml.qdoc index 54fa098..b767587 100644 --- a/doc/src/getting-started/gettingstartedqml.qdoc +++ b/doc/src/getting-started/gettingstartedqml.qdoc @@ -42,7 +42,7 @@ installation instructions and requirements for different platforms. Qt Quick includes a declarative language called - \l{Introduction to the QML language}{QML}, the \l{QtDeclarative Module}, and + \l{Introduction to the QML language}{QML}, the \l{Qt Declarative Module}, and \l{QML Viewer}. \section1 QML to Build User Interfaces diff --git a/doc/src/platforms/platform-notes.qdoc b/doc/src/platforms/platform-notes.qdoc index dbedc1d..9c5f3c8 100644 --- a/doc/src/platforms/platform-notes.qdoc +++ b/doc/src/platforms/platform-notes.qdoc @@ -739,6 +739,9 @@ \o \c AllFiles when \l{http://developer.symbian.org/wiki/index.php/Capabilities_%28Symbian_Signed%29/AllFiles_Capability}{accessing specific areas.} \row \o QtNetwork \o \c NetworkServices is basically always required for this module. + \o \c ReadUserData is required to include all the phone's SSL certificates in the system's default CA certificate list + (for example those added by the user or stored in the SIM card), + without this capability only the CA certs built into the phone are used. \row \o QtMultiMedia \o \c UserEnvironment if QAudioInput is used. \endtable diff --git a/doc/src/qt4-intro.qdoc b/doc/src/qt4-intro.qdoc index 2384051..62decbb 100644 --- a/doc/src/qt4-intro.qdoc +++ b/doc/src/qt4-intro.qdoc @@ -466,7 +466,7 @@ collaborate tightly and create animated and fluid user experiences, using existing knowledge in script language and design. - \i QtDeclarative is a C++ library that provides the underlying engine, + \i Qt Declarative is a C++ library that provides the underlying engine, which translates the declarative description of the UI in QML into items on a QGraphicsScene. The library also provides APIs to bind custom C++ types and elements to QML, and to connect the QML UI with diff --git a/doc/src/snippets/code/src_qtestlib_qtestcase.cpp b/doc/src/snippets/code/src_qtestlib_qtestcase.cpp index 6ae8939..adb0c34 100644 --- a/doc/src/snippets/code/src_qtestlib_qtestcase.cpp +++ b/doc/src/snippets/code/src_qtestlib_qtestcase.cpp @@ -230,5 +230,21 @@ widget.show(); QTest::qWaitForWindowShown(&widget); //! [24] +//! [25] +QWidget widget; + +QTest::touchEvent(&widget) + .press(0, QPoint(10, 10)); +QTest::touchEvent(&widget) + .stationary(0) + .press(1, QPoint(40, 10)); +QTest::touchEvent(&widget) + .move(0, QPoint(12, 12)) + .move(1, QPoint(45, 5)); +QTest::touchEvent(&widget) + .release(0) + .release(1); +//! [25] + } diff --git a/doc/src/snippets/declarative/loader/sizeitem.qml b/doc/src/snippets/declarative/loader/sizeitem.qml new file mode 100644 index 0000000..6ace8d6 --- /dev/null +++ b/doc/src/snippets/declarative/loader/sizeitem.qml @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +import QtQuick 1.0 + +Item { + width: 200; height: 200 + + Loader { + // position the Loader in the center of the parent + anchors.centerIn: parent + sourceComponent: rect + } + + Component { + id: rect + Rectangle { + width: 50 + height: 50 + color: "red" + } + } +} +//![0] diff --git a/doc/src/snippets/declarative/loader/sizeloader.qml b/doc/src/snippets/declarative/loader/sizeloader.qml new file mode 100644 index 0000000..eac7d57 --- /dev/null +++ b/doc/src/snippets/declarative/loader/sizeloader.qml @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +import QtQuick 1.0 + +Item { + width: 200; height: 200 + + Loader { + // Explicitly set the size of the Loader to the parent item's size + anchors.fill: parent + sourceComponent: rect + } + + Component { + id: rect + Rectangle { + width: 50 + height: 50 + color: "red" + } + } +} +//![0] diff --git a/doc/src/snippets/declarative/qtbinding/context-advanced/MyItem.qml b/doc/src/snippets/declarative/qtbinding/context-advanced/MyItem.qml new file mode 100644 index 0000000..607651a --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/context-advanced/MyItem.qml @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +// MyItem.qml +import QtQuick 1.0 + +Text { text: applicationData.getCurrentDateTime() } +//![0] + diff --git a/doc/src/snippets/declarative/qtbinding/context-advanced/applicationdata.h b/doc/src/snippets/declarative/qtbinding/context-advanced/applicationdata.h new file mode 100644 index 0000000..930e381 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/context-advanced/applicationdata.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QObject> +#include <QDateTime> + +//![0] +class ApplicationData : public QObject +{ + Q_OBJECT +public: + Q_INVOKABLE QDateTime getCurrentDateTime() const { + return QDateTime::currentDateTime(); + } +}; +//![0] diff --git a/doc/src/snippets/declarative/qtbinding/stopwatch/stopwatch.cpp b/doc/src/snippets/declarative/qtbinding/context-advanced/connections.qml index 124422f..749bea3 100644 --- a/doc/src/snippets/declarative/qtbinding/stopwatch/stopwatch.cpp +++ b/doc/src/snippets/declarative/qtbinding/context-advanced/connections.qml @@ -37,26 +37,16 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +import QtQuick 1.0 -#include "stopwatch.h" +//![0] +Text { + text: applicationData.getCurrentDateTime() -Stopwatch::Stopwatch() - : m_running(false) -{ -} - -bool Stopwatch::isRunning() const -{ - return m_running; -} - -void Stopwatch::start() -{ - m_running = true; -} - -void Stopwatch::stop() -{ - m_running = false; + Connections { + target: applicationData + onDataChanged: console.log("The application data changed!") + } } +//![0] diff --git a/doc/src/snippets/declarative/qtbinding/context-advanced/context-advanced.pro b/doc/src/snippets/declarative/qtbinding/context-advanced/context-advanced.pro new file mode 100644 index 0000000..188a9fb --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/context-advanced/context-advanced.pro @@ -0,0 +1,3 @@ +QT += declarative +SOURCES += main.cpp +HEADERS += applicationdata.h diff --git a/doc/src/snippets/declarative/qtbinding/custompalette/main.cpp b/doc/src/snippets/declarative/qtbinding/context-advanced/main.cpp index 7481b75..b92215b 100644 --- a/doc/src/snippets/declarative/qtbinding/custompalette/main.cpp +++ b/doc/src/snippets/declarative/qtbinding/context-advanced/main.cpp @@ -37,22 +37,21 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> -#include <QApplication> -#include <QDeclarativeView> -#include <QDeclarativeContext> - -#include "custompalette.h" +#include "applicationdata.h" //![0] -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { QApplication app(argc, argv); QDeclarativeView view; - view.rootContext()->setContextProperty("palette", new CustomPalette); - view.setSource(QUrl::fromLocalFile("main.qml")); + ApplicationData data; + view.rootContext()->setContextProperty("applicationData", &data); + + view.setSource(QUrl::fromLocalFile("MyItem.qml")); view.show(); return app.exec(); diff --git a/doc/src/snippets/declarative/qtbinding/context/MyItem.qml b/doc/src/snippets/declarative/qtbinding/context/MyItem.qml new file mode 100644 index 0000000..faa42dc --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/context/MyItem.qml @@ -0,0 +1,45 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +// MyItem.qml +import QtQuick 1.0 + +Text { text: currentDateTime } +//![0] diff --git a/doc/src/snippets/declarative/qtbinding/contextproperties/contextproperties.pro b/doc/src/snippets/declarative/qtbinding/context/context.pro index 68eeaf2..68eeaf2 100644 --- a/doc/src/snippets/declarative/qtbinding/contextproperties/contextproperties.pro +++ b/doc/src/snippets/declarative/qtbinding/context/context.pro diff --git a/doc/src/snippets/declarative/qtbinding/context/main.cpp b/doc/src/snippets/declarative/qtbinding/context/main.cpp new file mode 100644 index 0000000..bbe9956 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/context/main.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + +//![0] +QDeclarativeView view; +view.rootContext()->setContextProperty("currentDateTime", QDateTime::currentDateTime()); +view.setSource(QUrl::fromLocalFile("MyItem.qml")); +view.show(); +//![0] + + return app.exec(); +} + diff --git a/doc/src/snippets/declarative/qtbinding/custompalette/custompalette.h b/doc/src/snippets/declarative/qtbinding/custompalette/custompalette.h deleted file mode 100644 index 7e84bd4..0000000 --- a/doc/src/snippets/declarative/qtbinding/custompalette/custompalette.h +++ /dev/null @@ -1,79 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor -** the names of its contributors may be used to endorse or promote -** products derived from this software without specific prior written -** permission. -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QObject> -#include <QColor> - -//![0] -class CustomPalette : public QObject -{ - Q_OBJECT - Q_PROPERTY(QColor background READ background WRITE setBackground NOTIFY backgroundChanged) - Q_PROPERTY(QColor text READ text WRITE setText NOTIFY textChanged) - -public: - CustomPalette() : m_background(Qt::white), m_text(Qt::black) {} - - QColor background() const { return m_background; } - void setBackground(const QColor &c) { - if (c != m_background) { - m_background = c; - emit backgroundChanged(); - } - } - - QColor text() const { return m_text; } - void setText(const QColor &c) { - if (c != m_text) { - m_text = c; - emit textChanged(); - } - } - -signals: - void textChanged(); - void backgroundChanged(); - -private: - QColor m_background; - QColor m_text; -}; - -//![0] diff --git a/doc/src/snippets/declarative/qtbinding/custompalette/custompalette.pro b/doc/src/snippets/declarative/qtbinding/enums/enums.pro index e6af0d0..82bc123 100644 --- a/doc/src/snippets/declarative/qtbinding/custompalette/custompalette.pro +++ b/doc/src/snippets/declarative/qtbinding/enums/enums.pro @@ -1,3 +1,3 @@ QT += declarative -HEADERS += custompalette.h SOURCES += main.cpp +HEADERS += imageviewer.h diff --git a/doc/src/snippets/declarative/qtbinding/enums/imageviewer.h b/doc/src/snippets/declarative/qtbinding/enums/imageviewer.h new file mode 100644 index 0000000..8aaddee --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/enums/imageviewer.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> + +//![start] +class ImageViewer : public QDeclarativeItem +{ + Q_OBJECT + Q_ENUMS(Status) + Q_PROPERTY(Status status READ status NOTIFY statusChanged) +public: + enum Status { + Ready, + Loading, + Error + }; + + Status status() const; +//![start] + + ImageViewer(QDeclarativeItem *parent = 0); + +public slots: + void emitSignals(); + +//![end] +signals: + void statusChanged(); +}; +//![end] diff --git a/doc/src/snippets/declarative/qtbinding/enums/main.cpp b/doc/src/snippets/declarative/qtbinding/enums/main.cpp new file mode 100644 index 0000000..9243f4b --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/enums/main.cpp @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> + +#include "imageviewer.h" + +ImageViewer::ImageViewer(QDeclarativeItem *parent) + : QDeclarativeItem(parent) +{ + QTimer::singleShot(0, this, SLOT(emitSignals())); +} + +ImageViewer::Status ImageViewer::status() const +{ + return Ready; +} + +void ImageViewer::emitSignals() +{ + emit statusChanged(); +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + qmlRegisterType<ImageViewer>("MyLibrary", 1, 0, "ImageViewer"); + + QDeclarativeView view; + view.setSource(QUrl::fromLocalFile("standalone.qml")); + view.show(); + + return app.exec(); +} + diff --git a/doc/src/snippets/declarative/qtbinding/enums/standalone.qml b/doc/src/snippets/declarative/qtbinding/enums/standalone.qml new file mode 100644 index 0000000..ad6c14c --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/enums/standalone.qml @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +import MyLibrary 1.0 + +//![0] +ImageViewer { + onStatusChanged: { + if (status == ImageViewer.Ready) + console.log("Image viewer is ready!") + } +} +//![0] diff --git a/doc/src/snippets/declarative/qtbinding/stopwatch/main.qml b/doc/src/snippets/declarative/qtbinding/functions-cpp/MyItem.qml index f894f71..8d8cd56 100644 --- a/doc/src/snippets/declarative/qtbinding/stopwatch/main.qml +++ b/doc/src/snippets/declarative/qtbinding/functions-cpp/MyItem.qml @@ -37,21 +37,18 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ - //![0] +// MyItem.qml import QtQuick 1.0 -Rectangle { - width: 300 - height: 300 +Item { + width: 100; height: 100 MouseArea { anchors.fill: parent onClicked: { - if (stopwatch.isRunning()) - stopwatch.stop() - else - stopwatch.start(); + myObject.cppMethod("Hello from QML") + myObject.cppSlot(12345) } } } diff --git a/doc/src/snippets/declarative/qtbinding/functions-cpp/functions-qml.pro b/doc/src/snippets/declarative/qtbinding/functions-cpp/functions-qml.pro new file mode 100644 index 0000000..01066c1 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/functions-cpp/functions-qml.pro @@ -0,0 +1,3 @@ +QT += declarative +SOURCES += main.cpp +HEADERS += myclass.h diff --git a/doc/src/snippets/declarative/qtbinding/stopwatch/main.cpp b/doc/src/snippets/declarative/qtbinding/functions-cpp/main.cpp index 12481da..91d6aec 100644 --- a/doc/src/snippets/declarative/qtbinding/stopwatch/main.cpp +++ b/doc/src/snippets/declarative/qtbinding/functions-cpp/main.cpp @@ -37,23 +37,20 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> -#include "stopwatch.h" - -#include <QDeclarativeView> -#include <QDeclarativeContext> -#include <QApplication> +#include "myclass.h" //![0] -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { QApplication app(argc, argv); QDeclarativeView view; - view.rootContext()->setContextProperty("stopwatch", - new Stopwatch); + MyClass myClass; + view.rootContext()->setContextProperty("myObject", &myClass); - view.setSource(QUrl::fromLocalFile("main.qml")); + view.setSource(QUrl::fromLocalFile("MyItem.qml")); view.show(); return app.exec(); diff --git a/doc/src/snippets/declarative/qtbinding/custompalette/main.qml b/doc/src/snippets/declarative/qtbinding/functions-cpp/myclass.h index a20d9e0..76c628e 100644 --- a/doc/src/snippets/declarative/qtbinding/custompalette/main.qml +++ b/doc/src/snippets/declarative/qtbinding/functions-cpp/myclass.h @@ -37,26 +37,21 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include <QObject> +#include <QDebug> //![0] -import QtQuick 1.0 - -Rectangle { - width: 240 - height: 320 - color: palette.background - - Text { - anchors.centerIn: parent - color: palette.text - text: "Click me to change color!" +class MyClass : public QObject +{ + Q_OBJECT +public: + Q_INVOKABLE void cppMethod(const QString &msg) { + qDebug() << "Called the C++ method with" << msg; } - MouseArea { - anchors.fill: parent - onClicked: { - palette.text = "blue"; - } +public slots: + void cppSlot(int number) { + qDebug() << "Called the C++ slot with" << number; } -} +}; //![0] diff --git a/doc/src/snippets/declarative/qtbinding/contextproperties/main.qml b/doc/src/snippets/declarative/qtbinding/functions-qml/MyItem.qml index 425346d..6161d6e 100644 --- a/doc/src/snippets/declarative/qtbinding/contextproperties/main.qml +++ b/doc/src/snippets/declarative/qtbinding/functions-qml/MyItem.qml @@ -37,19 +37,14 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ - //![0] +// MyItem.qml import QtQuick 1.0 -Rectangle { - width: 300 - height: 300 - - color: backgroundColor - - Text { - anchors.centerIn: parent - text: "Hello Yellow World!" +Item { + function myQmlFunction(msg) { + console.log("Got message:", msg) + return "some return value" } } //![0] diff --git a/doc/src/snippets/declarative/qtbinding/functions-qml/functions-qml.pro b/doc/src/snippets/declarative/qtbinding/functions-qml/functions-qml.pro new file mode 100644 index 0000000..68eeaf2 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/functions-qml/functions-qml.pro @@ -0,0 +1,2 @@ +QT += declarative +SOURCES += main.cpp diff --git a/doc/src/snippets/declarative/qtbinding/functions-qml/main.cpp b/doc/src/snippets/declarative/qtbinding/functions-qml/main.cpp new file mode 100644 index 0000000..3e9e51e --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/functions-qml/main.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + +//![0] +// main.cpp +QDeclarativeEngine engine; +QDeclarativeComponent component(&engine, "MyItem.qml"); +QObject *object = component.create(); + +QVariant returnedValue; +QVariant msg = "Hello from C++"; +QMetaObject::invokeMethod(object, "myQmlFunction", + Q_RETURN_ARG(QVariant, returnedValue), + Q_ARG(QVariant, msg)); + +qDebug() << "QML function returned:" << returnedValue.toString(); +delete object; +//![0] +} + diff --git a/doc/src/snippets/declarative/qtbinding/loading/MyItem.qml b/doc/src/snippets/declarative/qtbinding/loading/MyItem.qml new file mode 100644 index 0000000..40138f6 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/loading/MyItem.qml @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![start] +import QtQuick 1.0 + +Item { + width: 100; height: 100 +//![start] + +//![child] + Rectangle { + anchors.fill: parent + objectName: "rect" + } +//![child] + +//![end] +} +//![end] diff --git a/doc/src/snippets/declarative/qtbinding/loading/loading.pro b/doc/src/snippets/declarative/qtbinding/loading/loading.pro new file mode 100644 index 0000000..68eeaf2 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/loading/loading.pro @@ -0,0 +1,2 @@ +QT += declarative +SOURCES += main.cpp diff --git a/doc/src/snippets/declarative/qtbinding/loading/main.cpp b/doc/src/snippets/declarative/qtbinding/loading/main.cpp new file mode 100644 index 0000000..bd400b0 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/loading/main.cpp @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> + +static void withComponent() +{ +//![QDeclarativeComponent-a] +// Using QDeclarativeComponent +QDeclarativeEngine engine; +QDeclarativeComponent component(&engine, + QUrl::fromLocalFile("MyItem.qml")); +QObject *object = component.create(); +//![QDeclarativeComponent-a] +} + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + +//![QDeclarativeView] +// Using QDeclarativeView +QDeclarativeView view; +view.setSource(QUrl::fromLocalFile("MyItem.qml")); +view.show(); +QObject *object = view.rootObject(); +//![QDeclarativeView] + +//![properties] +object->setProperty("width", 500); +QDeclarativeProperty(object, "width").write(500); +//![properties] + +//![cast] +QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(object); +item->setWidth(500); +//![cast] + +//![findChild] +QObject *rect = object->findChild<QObject*>("rect"); +if (rect) + rect->setProperty("color", "red"); +//![findChild] + +//![QDeclarativeComponent-b] +delete object; +//![QDeclarativeComponent-b] + +withComponent(); + + return app.exec(); +} + diff --git a/doc/src/snippets/declarative/qtbinding/newelements/imageviewer.h b/doc/src/snippets/declarative/qtbinding/newelements/imageviewer.h new file mode 100644 index 0000000..fd9db5e --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/newelements/imageviewer.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> + +//![0] +class ImageViewer : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(QUrl image READ image WRITE setImage NOTIFY imageChanged) + +public: + void setImage(const QUrl &url); + QUrl image() const; + +signals: + void imageChanged(); +}; +//![0] diff --git a/doc/src/snippets/declarative/qtbinding/newelements/main.cpp b/doc/src/snippets/declarative/qtbinding/newelements/main.cpp new file mode 100644 index 0000000..57994ca --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/newelements/main.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> + +#include "imageviewer.h" + +void ImageViewer::setImage(const QUrl &url) {} +QUrl ImageViewer::image() const { return QUrl(); } + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + //![register] + qmlRegisterType<ImageViewer>("MyLibrary", 1, 0, "ImageViewer"); + //![register] + + QDeclarativeView view; + view.setSource(QUrl::fromLocalFile("standalone.qml")); + view.show(); + + return app.exec(); +} + diff --git a/doc/src/snippets/declarative/qtbinding/newelements/newelements.pro b/doc/src/snippets/declarative/qtbinding/newelements/newelements.pro new file mode 100644 index 0000000..82bc123 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/newelements/newelements.pro @@ -0,0 +1,3 @@ +QT += declarative +SOURCES += main.cpp +HEADERS += imageviewer.h diff --git a/doc/src/snippets/declarative/qtbinding/newelements/standalone.qml b/doc/src/snippets/declarative/qtbinding/newelements/standalone.qml new file mode 100644 index 0000000..7345f21 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/newelements/standalone.qml @@ -0,0 +1,44 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +import MyLibrary 1.0 + +ImageViewer { image: "smile.png" } +//![0] diff --git a/doc/src/snippets/declarative/qtbinding/properties-cpp/MyItem.qml b/doc/src/snippets/declarative/qtbinding/properties-cpp/MyItem.qml new file mode 100644 index 0000000..9db4b93 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/properties-cpp/MyItem.qml @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +// MyItem.qml +import QtQuick 1.0 + +Rectangle { + width: 100; height: 100 + color: applicationData.backgroundColor + + MouseArea { + anchors.fill: parent + onClicked: applicationData.backgroundColor = "red" + } +} +//![0] + diff --git a/doc/src/snippets/declarative/qtbinding/properties-cpp/applicationdata.h b/doc/src/snippets/declarative/qtbinding/properties-cpp/applicationdata.h new file mode 100644 index 0000000..763a451 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/properties-cpp/applicationdata.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QObject> +#include <QColor> + +//![0] +class ApplicationData : public QObject +{ + Q_OBJECT + Q_PROPERTY(QColor backgroundColor + READ backgroundColor + WRITE setBackgroundColor + NOTIFY backgroundColorChanged) + +public: + void setBackgroundColor(const QColor &c) { + if (c != m_color) { + m_color = c; + emit backgroundColorChanged(); + } + } + + QColor backgroundColor() const { + return m_color; + } + +signals: + void backgroundColorChanged(); + +private: + QColor m_color; +}; +//![0] diff --git a/doc/src/snippets/declarative/qtbinding/properties-cpp/main.cpp b/doc/src/snippets/declarative/qtbinding/properties-cpp/main.cpp new file mode 100644 index 0000000..36b508d --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/properties-cpp/main.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> + +#include "applicationdata.h" + +int main(int argc, char *argv[]) { + QApplication app(argc, argv); + + QDeclarativeView view; + + ApplicationData data; + view.rootContext()->setContextProperty("applicationData", &data); + + view.setSource(QUrl::fromLocalFile("MyItem.qml")); + view.show(); + + return app.exec(); +} + diff --git a/doc/src/snippets/declarative/qtbinding/properties-cpp/properties-cpp.pro b/doc/src/snippets/declarative/qtbinding/properties-cpp/properties-cpp.pro new file mode 100644 index 0000000..188a9fb --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/properties-cpp/properties-cpp.pro @@ -0,0 +1,3 @@ +QT += declarative +SOURCES += main.cpp +HEADERS += applicationdata.h diff --git a/doc/src/snippets/declarative/qtbinding/properties-qml/MyItem.qml b/doc/src/snippets/declarative/qtbinding/properties-qml/MyItem.qml new file mode 100644 index 0000000..a43ded8 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/properties-qml/MyItem.qml @@ -0,0 +1,47 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +// MyItem.qml +import QtQuick 1.0 + +Item { + property int someNumber: 100 +} +//![0] diff --git a/doc/src/snippets/declarative/qtbinding/properties-qml/main.cpp b/doc/src/snippets/declarative/qtbinding/properties-qml/main.cpp new file mode 100644 index 0000000..d8daff8 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/properties-qml/main.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + +//![0] +QDeclarativeEngine engine; +QDeclarativeComponent component(&engine, "MyItem.qml"); +QObject *object = component.create(); + +qDebug() << "Property value:" << QDeclarativeProperty::read(object, "someNumber").toInt(); +QDeclarativeProperty::write(object, "someNumber", 5000); + +qDebug() << "Property value:" << object->property("someNumber").toInt(); +object->setProperty("someNumber", 100); +//![0] + + return app.exec(); +} + diff --git a/doc/src/snippets/declarative/qtbinding/properties-qml/properties-qml.pro b/doc/src/snippets/declarative/qtbinding/properties-qml/properties-qml.pro new file mode 100644 index 0000000..68eeaf2 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/properties-qml/properties-qml.pro @@ -0,0 +1,2 @@ +QT += declarative +SOURCES += main.cpp diff --git a/doc/src/snippets/declarative/qtbinding/resources/main.qml b/doc/src/snippets/declarative/qtbinding/resources/main.qml index 644d963..43029cf 100644 --- a/doc/src/snippets/declarative/qtbinding/resources/main.qml +++ b/doc/src/snippets/declarative/qtbinding/resources/main.qml @@ -39,9 +39,8 @@ ****************************************************************************/ //![0] +// main.qml import QtQuick 1.0 -Image { - source: "images/background.png" -} +Image { source: "images/background.png" } //![0] diff --git a/doc/src/snippets/declarative/qtbinding/signals-cpp/MyItem.qml b/doc/src/snippets/declarative/qtbinding/signals-cpp/MyItem.qml new file mode 100644 index 0000000..d900830 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/signals-cpp/MyItem.qml @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +// MyItem.qml +import QtQuick 1.0 + +Item { + Connections { + target: imageViewer + onImageChanged: console.log("Image has changed!") + } +} +//![0] diff --git a/doc/src/snippets/declarative/qtbinding/signals-cpp/imageviewer.h b/doc/src/snippets/declarative/qtbinding/signals-cpp/imageviewer.h new file mode 100644 index 0000000..7288d11 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/signals-cpp/imageviewer.h @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> + +//![start] +class ImageViewer : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(QUrl image READ image WRITE setImage NOTIFY imageChanged) +public: +//![start] + ImageViewer(QDeclarativeItem *item = 0); + + void setImage(const QUrl &url) {} + QUrl image() const { return QUrl(); } + +public slots: + void emitSignals(); + +//![end] +signals: + void imageChanged(); + void loadingError(const QString &errorMsg); +}; +//![end] + diff --git a/doc/src/snippets/declarative/qtbinding/contextproperties/main.cpp b/doc/src/snippets/declarative/qtbinding/signals-cpp/main.cpp index 6887e8f..e5dd33c 100644 --- a/doc/src/snippets/declarative/qtbinding/contextproperties/main.cpp +++ b/doc/src/snippets/declarative/qtbinding/signals-cpp/main.cpp @@ -37,42 +37,45 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> -#include <QDeclarativeComponent> -#include <QDeclarativeEngine> +#include "imageviewer.h" -//![0] -#include <QApplication> -#include <QDeclarativeView> -#include <QDeclarativeContext> + +ImageViewer::ImageViewer(QDeclarativeItem *item) + : QDeclarativeItem(item) +{ + QTimer::singleShot(0, this, SLOT(emitSignals())); +} + +void ImageViewer::emitSignals() +{ + emit imageChanged(); + emit loadingError("some error message"); +} int main(int argc, char *argv[]) { QApplication app(argc, argv); - QDeclarativeView view; - QDeclarativeContext *context = view.rootContext(); - context->setContextProperty("backgroundColor", - QColor(Qt::yellow)); + qmlRegisterType<ImageViewer>("MyLibrary", 1, 0, "ImageViewer"); - view.setSource(QUrl::fromLocalFile("main.qml")); - view.show(); + QDeclarativeView standalone; + standalone.setSource(QUrl::fromLocalFile("standalone.qml")); + standalone.show(); - return app.exec(); -} -//![0] +//![connections] +ImageViewer viewer; -static void alternative() -{ - // Alternatively, if we don't actually want to display main.qml: -//![1] - QDeclarativeEngine engine; - QDeclarativeContext *windowContext = new QDeclarativeContext(engine.rootContext()); - windowContext->setContextProperty("backgroundColor", QColor(Qt::yellow)); +QDeclarativeView view; +view.rootContext()->setContextProperty("imageViewer", &viewer); - QDeclarativeComponent component(&engine, "main.qml"); - QObject *window = component.create(windowContext); -//![1] +view.setSource(QUrl::fromLocalFile("MyItem.qml")); +view.show(); +//![connections] + + return app.exec(); } diff --git a/doc/src/snippets/declarative/qtbinding/signals-cpp/signals-cpp.pro b/doc/src/snippets/declarative/qtbinding/signals-cpp/signals-cpp.pro new file mode 100644 index 0000000..9aa8702 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/signals-cpp/signals-cpp.pro @@ -0,0 +1,3 @@ +QT += declarative +SOURCES += main.cpp +HEADERS += "imageviewer.h" diff --git a/doc/src/snippets/declarative/qtbinding/signals-cpp/standalone.qml b/doc/src/snippets/declarative/qtbinding/signals-cpp/standalone.qml new file mode 100644 index 0000000..4027b19 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/signals-cpp/standalone.qml @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +import MyLibrary 1.0 + +//![0] +ImageViewer { + onImageChanged: console.log("Image changed!") + onLoadingError: console.log("Image failed to load:", errorMsg) +} +//![0] + diff --git a/doc/src/snippets/declarative/qtbinding/signals-qml/MyItem.qml b/doc/src/snippets/declarative/qtbinding/signals-qml/MyItem.qml new file mode 100644 index 0000000..e4b6ced --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/signals-qml/MyItem.qml @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +//![0] +// MyItem.qml +import QtQuick 1.0 + +Item { + id: item + width: 100; height: 100 + + signal qmlSignal(string msg) + + MouseArea { + anchors.fill: parent + onClicked: item.qmlSignal("Hello from QML") + } +} +//![0] diff --git a/doc/src/snippets/declarative/qtbinding/signals-qml/main.cpp b/doc/src/snippets/declarative/qtbinding/signals-qml/main.cpp new file mode 100644 index 0000000..f1d03c4 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/signals-qml/main.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor +** the names of its contributors may be used to endorse or promote +** products derived from this software without specific prior written +** permission. +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtCore> +#include <QtDeclarative> + +//![0] +int main(int argc, char *argv[]) { + QApplication app(argc, argv); + + QDeclarativeView view(QUrl::fromLocalFile("MyItem.qml")); + QObject *item = view.rootObject(); + + MyClass myClass; + QObject::connect(item, SIGNAL(qmlSignal(QString)), + &myClass, SLOT(cppSlot(QString))); + + view.show(); + return app.exec(); +} +//![0] + +#include "moc_main.cpp" diff --git a/doc/src/snippets/declarative/qtbinding/stopwatch/stopwatch.h b/doc/src/snippets/declarative/qtbinding/signals-qml/myclass.h index fd65916..c326a54 100644 --- a/doc/src/snippets/declarative/qtbinding/stopwatch/stopwatch.h +++ b/doc/src/snippets/declarative/qtbinding/signals-qml/myclass.h @@ -37,25 +37,15 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ - #include <QObject> - +#include <QDebug> //![0] -class Stopwatch : public QObject +class MyClass : public QObject { Q_OBJECT -public: - Stopwatch(); - - Q_INVOKABLE bool isRunning() const; - public slots: - void start(); - void stop(); - -private: - bool m_running; + void cppSlot(const QString &msg) { + qDebug() << "Called the C++ slot with message:" << msg; + } }; - //![0] - diff --git a/doc/src/snippets/declarative/qtbinding/signals-qml/signals-qml.pro b/doc/src/snippets/declarative/qtbinding/signals-qml/signals-qml.pro new file mode 100644 index 0000000..01066c1 --- /dev/null +++ b/doc/src/snippets/declarative/qtbinding/signals-qml/signals-qml.pro @@ -0,0 +1,3 @@ +QT += declarative +SOURCES += main.cpp +HEADERS += myclass.h diff --git a/doc/src/snippets/declarative/qtbinding/stopwatch/stopwatch.pro b/doc/src/snippets/declarative/qtbinding/stopwatch/stopwatch.pro deleted file mode 100644 index d803e6a..0000000 --- a/doc/src/snippets/declarative/qtbinding/stopwatch/stopwatch.pro +++ /dev/null @@ -1,3 +0,0 @@ -QT += declarative -HEADERS += stopwatch.h -SOURCES += main.cpp stopwatch.cpp |