Use QML to develop MeeGo applications

Source: Internet
Author: User
Tags qt designer

Qt Declarative UI is gaining popularity, but there is little Chinese information about what kind of technology it is and how to use it. A few articles can be searched occasionally, which is confusing. This getting started tutorial is from the official Qt documentation and is more about syntax.

What is QML?

QML is a declarative and scripting language that describes the application UI. the end of qml includes the application appearance menu, buttons, layout, and so on) and the Action click event. In QML, the UI interface is described as a tree structure with attribute objects. Understanding HTML, CSS, and other Web technologies is helpful, but not necessary. The syntax format is very similar to CSS (refer to the example of stationery), but it also supports Programming Control in the javacript format.

In the first two paragraphs officially introduced above, QML is actually a new feature in Qt QuickQt4.7.0) one of the core components: Qt Quick is a group designed to help developers create on mobile phones, media players, set-top boxes and other portable devices use more and more intuitive, modern, and smooth UI tools. Qt Quick includes a rich set of user interface elements, a declarative language QML used to describe the user interface) and runtime, A group of C ++ APIs used to integrate these high-level features into classic Qt applications.

From the official introduction, we can see that Qt Quick is tailored for the rapid development of mobile platforms. Let's take a look at a practical example: Running MeeNotes on MeeGo, in addition to the business logic, the UI is implemented using QML.


MeeNotes Running Effect


Screen Switching


Running effects in simulators

MeeNotes can be found here: Use git to clone qt-components and meenotes, and then compile qt-components first. This depends on qt4.7, which is the key to rapid development of meego applications using QML, it implements a set of meego QML modules and can compile and run MeeNotes. If it cannot run, check whether the Qt installation directory contains com. nokia. meego this module my located at/usr/local/Trolltech/Qt-4.7.0/imports/com/meego) if not, you need to manually execute qmake/make install in src/MeeGo under the decompressed directory of qt-components for compilation and installation.

Let's take a look at the actual code in MeeNotes.

The red part of src. pro under the src directory is different from the development using libmeegotouch:

 
 
  1. TEMPLATE = app    
  2. TARGET = ../MeeNotes    
  3. LIBS += -lQtComponents      
  4. HEADERS += models/meenotesmodel.h \    
  5.           models/notemodel.h    
  6. SOURCES += main.cpp \    
  7.           models/meenotesmodel.cpp \    
  8.           models/notemodel.cpp    
  9. QT += declarative  

Let's look at main. cpp at the main entry:

 
 
  1. # Include "models/meenotesmodel. h"
  2. # Include <QApplication>
  3. # Include <QDeclarativeContext>
  4. # Include <QDeclarativeComponent>
  5. # Include <QDir>
  6. # Include <QtComponents/qdeclarativewindow. h>
  7. Int main (int argc, char ** argv)
  8. {
  9. QApplication app (argc, argv );
  10. App. setApplicationName ("MeeNotes ");
  11. // MeeNotesModel is a Model class
  12. QmlRegisterType <NoteModel> ();
  13. MeeNotesModel model;
  14. Model. setSource ("notes /");
  15. // Where can I find main. qml?
  16. # Ifndef MEENOTES_RESOURCE_DIR
  17. Const QDir dir (QApplication: applicationDirPath ());
  18. Const QUrl qmlPath (dir. absoluteFilePath ("resource/default/main. qml "));
  19. # Else
  20. Const QDir dir (MEENOTES_RESOURCE_DIR );
  21. Const QUrl qmlPath (dir. absoluteFilePath ("main. qml "));
  22. # Endif
  23. // Create a window that can explain qml running
  24. QDeclarativeWindow window (qmlPath );
  25. Window. rootContext ()-> setContextProperty ("meenotes", & model );
  26. Window. window ()-> show ();
  27. Return app.exe c ();
  28. }

View main. qml:

 
 
  1. import Qt 4.7    
  2. import com.meego 1.0      
  3. Window {    
  4.    id: window    
  5.    Component.onCompleted: {    
  6.        window.nextPage(Qt.createComponent("NoteList.qml"))      
  7.    }    
  8. }  

View NoteList. qml:

 
 
  1. Import Qt 4.7
  2. Import com. meego 1.0
  3. Page {
  4. Title: "MeeNotes"
  5. Actions :[
  6. Action {
  7. IconId: "icon-m-toolbar-add" // handle the new note button
  8. OnTriggered :{
  9. Var note = meenotes. newNote ();
  10. Note. title = (Math. random ()> 0.5 )? "Cool title! ":"";
  11. ViewNote (note );
  12. }
  13. },
  14. Action {
  15. IconId: "icon-m-toolbar-favorite-mark" // button processing for horizontal and vertical screen Switching
  16. OnTriggered :{
  17. Screenscreen. orientation = screen. orientation = Screen. Portrait? Screen. Landscape: Screen. Portrait;
  18. }
  19. }
  20. ]
  21. Component {
  22. ... ... ... // Omitted
  23. }
  24. Rectangle {
  25. Color: "white"
  26. Anchors. fill: parent
  27. }
  28. GridView {
  29. Id: grid
  30. Anchors. fill: parent
  31. Model: meenotes
  32. CellWidth: 250
  33. CellHeight: 210
  34. Delegate: noteDelegate
  35. }
  36. Function viewNote (note ){
  37. Window. nextPage (Qt. createComponent ("Note. qml "));
  38. Window. currentPage. note = note;
  39. }
  40. }

Given that QML is very efficient in writing css similar to web pages, it seems that Qt Designer has been ignored. In fact, Desinger of Version 2.01 can be previewed using the meegotouch style, the effect is as follows:


Currently, the Designer cannot directly generate the QML file. The Desinger of the next version is included in the plan. You can call it Qt Quick Designer, which can already be reflected in Qt Roadmap:

Qt Quick Designer

This is a tutorial on developing the MeeGo application using the QML language.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.