== App Developer Week -- Qt Quick: QML the Language -- jryannel -- Wed, Apr 13th, 2011 == {{{#!irc [17:01] jryannel, from the makers of Qt knows all about it and will be thrilled to tell you [17:01] Logs for this session will be available at http://irclogs.ubuntu.com/2011/04/13/%23ubuntu-classroom.html following the conclusion of the session. [17:02] Ok. Hi I'm jryannel from QtDF acting as Training Manager. This is my first irc training session so bear with me [17:02] I want to give short intro to Qt Quick's QML language and how to use it [17:02] First things first [17:03] You need Qt + Qt Creator, easiest [17:03] install - QtSDK (http://labs.qt.nokia.com/2011/04/06/qt-sdk-1-1-rc-released/) [17:03] or on ubuntu search for apt-cache search qt4; apt-cache search creator [17:04] To run QtQuick you need Qt 4.7.0 or better, latest is Qt 4.7.3 [17:04] or grab it from gitorious [17:04] So what is Qt Quick? [17:05] It's based on GraphicsView as 2.5d canvas API with graphics items [17:05] the graphicsview was developed in c++ and creating rich ui takes a long time [17:06] So our developers came up with a declarative way. [17:06] It looks like CSS or JSON [17:06] import QtQuick 1.0 [17:06] Rectangle { [17:06] width: 360 [17:06] height: 360 [17:06] } [17:07] First you import QtQuick module with major and minor version than you define a root element. [17:07] In this case a rectangle with a width of 360 and height of 360. [17:08] But live is easier if I don't type a book here; [17:08] Please visit: http://developer.qt.nokia.com/wiki/Qt_Quick_Tutorial_Basics [17:08] I will walk you through this and then another example [17:09] the qmlviewer is the declarative runner for developing purposes [17:09] it has many options, which you can ask with --help [17:09] It comes with Qt (in the bin folder) [17:10] qmlviewer main.qml will interpret the qml file and show the ui [17:10] The qmlviewer is made with the Qt Declarative module [17:10] a C++ module. You can write your own declarative viewer [17:11] see here how: http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeview.html#details [17:11] It's 3 lines of code [17:11] QDeclarativeView *view = new QDeclarativeView; [17:11] view->setSource(QUrl::fromLocalFile("myqmlfile.qml")); [17:11] view->show(); [17:12] That's in essence it the qmlviewer, which interprets the qml file. [17:12] But back to the qml side of coding [17:13] width : 300 means a property binding [17:14] It's different from assignment. It's kind of a contract. [17:14] So you can write "width: 2 * height" [17:14] with this width is always 2 times the height. [17:15] When the height changes the right side will be re-evaluated and assigned to the left [17:15] Another aspect width: 2 * height" shows: [17:15] JavaScript. Every right side can contain any kind of javascript code [17:16] A good javascript tutorial is at [17:16] https://developer.mozilla.org/en/JavaScript [17:17] Qt Quick is finally a declartive UI (QML) with C++ backend and JavaScript enhanced [17:18] Let's jump to "Composition of Components" on the wiki page I posted earlier [17:18] You can nest qml elements [17:18] A element has a name and propertis and child elments, like HTMl has [17:19] (I hope someone is correcting my spelling mistakes later :) ) [17:19] / File: BasicSteps_2.qml [17:19] import Qt 4.7 [17:19] [17:19] Rectangle { [17:19] width: 300 [17:19] height: 300 [17:19] color: "#FFF8DC" // cornsilk [17:19] [17:19] Image { [17:19] x: 10; y: 45 [17:20] source: "voringsfossen1.jpg" [17:20] } [17:20] [17:20] Text { [17:20] x: 10; y: 265 [17:20] text: "Voringsfossen" [17:20] } [17:20] } [17:20] ohh, sorry [17:20] so we have a rectangle with an image and text child [17:20] The child elements have a corrdinate system relative to the parents [17:21] so a x:10, means 10 px relative to parent. [17:22] E.g. all elements are derived from the Item element: http://doc.qt.nokia.com/4.7-snapshot/qml-item.html [17:22] You find an overview about which items are available here: http://doc.qt.nokia.com/4.7-snapshot/qmlbasicelements.html [17:23] so when you write "x:10" you actually overwrite the default property value (in this case "x:0"). [17:24] For example if you don't specify a width or height to an element, it will be invisible! [17:25] Let's come back to "http://developer.qt.nokia.com/wiki/Qt_Quick_Tutorial_Basics" -> Custom Properties [17:26] besides the default properties you can add own properties. [17:26] E.g. "property int frameSize: 300" [17:27] which data types are supported? See here: http://doc.qt.nokia.com/4.7-snapshot/qdeclarativebasictypes.html [17:27] Can I add own data types. Sure. You can extend Qt Quick from the C++ side: http://doc.qt.nokia.com/4.7-snapshot/qml-extending.html [17:28] Even you can write own elements. E.g. a chart type or everything what can be painted. [17:29] Besides visual items you can also use non-visual items, e.g. timer (http://doc.qt.nokia.com/4.7-snapshot/qml-timer.html) [17:30] And you can also push QObjects from the c++ world into Qt Quick, e.g. for example for a D-Bus binding, or... [17:30] Now visit shortly http://doc.qt.nokia.com/4.7-snapshot/qml-tutorial1.html for another start into qt quick [17:31] It's just another hello world in qt quick [17:32] In Qt Creator our ide you can create a qt quick project with "New Project" -> "Qt Quick UI" [17:32] If you choose Qt Quick Application, it will generate a C++ qml viewer for you additional [17:33] So just copy the helloworld from (http://doc.qt.nokia.com/4.7-snapshot/qml-tutorial1.html) in your new project and run the project (no need for compilation) [17:34] Qt Creator (http://doc.qt.nokia.com/qtcreator-snapshot/index.html) is very much keyboard driven [17:34] It has also support got git, mercurial and I think svn and some other version control systems [17:35] Also for pastebin. Which I need to remember to use more [17:35] Here is a list of shortcuts: http://doc.qt.nokia.com/qtcreator-snapshot/creator-keyboard-shortcuts.html [17:36] One of our partners has created a reference card: http://www.kdab.com/index.php?option=com_content&view=article&id=126 [17:36] for Qt Creator. [17:36] But now back to qml... [17:37] http://doc.qt.nokia.com/4.7-snapshot/qml-tutorial2.html shows how to create a custom qml component [17:37] A component is a piece of qml code in a separate qml file [17:38] E.g. if you want create your own button you write a "Button.qml" file [17:38] Inside would be possible a Rectangle a Mousearea (which receives mouse clicks). [17:39] In the example tutorial they create a Cell in a "Cell.qml" file [17:39] The signal keyword allows to emit signal from a qml element. Just by calling the function name [17:40] The id property is a special property. It allows to identify an element inside a qml file ... [17:40] ... but also across other qml files. [17:40] As a convention I always give my root element the "id: root" [17:41] I give application wide used components a nice name so that I can reference them from other qml files. [17:42] When referencing an id from another qml file you make yourself depending. So think about if you really want to be dependent on the other qml (component). [17:42] Id's are only known to the qml runtime when the qml file is actually loaded [17:42] Have a look at the "The main QML file" section [17:43] There we use the "Cell.qml" component with a simple call to "Cell". [17:43] Any qml file (with upper-camelcase" ) in the same folder can be used directly. (Others need to imported) [17:44] "onClicked: helloText" - is a handler to the signal clicked() from Cell.qml [17:44] Great. We can make some rectangles and text and place them somewhere so what? [17:45] With a little bit of C++ and some motivated developers you can make something more ... [17:45] ... see here http://labs.qt.nokia.com/2011/03/10/qml-components-for-desktop/ [17:46] Jens presents the desktop components (a little bit more complicated Cell.qml with some C++ backend) [17:48] It's a research project (that's why it's on labs). But I think they will be happy about feewdback [17:48] But in general Qt Quick is a very much design driven easy to code ui language. Where the heavy lifting is done in C++ [17:49] It's very flexible. With flexibility also comes a price [17:50] There are many solutions to the same problem. So you need a good practice to master the different elements and C++ extension possibilities to come to a good solution [17:50] Not every solution is simple and beautiful [17:51] There are 10 minutes remaining in the current session. [17:51] http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeexamples.html here you find more examples [17:52] Tomorrow we will go into the dynamic side of Qt Quick. Animations - States - Transitions [17:53] You can find learning material (e.g. videos and whole courses) here: http://qt.nokia.com/developer/learning/elearning [17:53] I hope I haven't confused you all to much. Qml is simple [17:53] Elements, Properties, Child Elements, [17:54] Property Binding [17:54] and JavaScript + C++ for application logic. [17:54] Take care. [17:54] Python and Qt Quick. Yes [17:55] There is PyQt and PySide [17:55] checkout: http://developer.qt.nokia.com/wiki/Category:LanguageBindings::PySide [17:56] There are 5 minutes remaining in the current session. [17:56] Sorry question was: Can I use Qt Quick with a python backend instead of C++? }}}