QML overview
Contents
In this post, I want to make a clear framewok of QML language howerver, this post is far from completed, I will update this post when I have new things.
Overview of QML language
In this post, I want to make a clear framewok of QML language howerver, this post is far from completed, I will update this post when I have new things.
Qt QML module is provides the QML engine and language infrastructure. Qt QML provides two API:
Qt Quick module is the standard library for writing for QML applications,Providing all the basic types for creating user interfaces with QML.
Qt Quick Control module provides a set of controls can be used to build complete interface in Qt Quick.
Qt Quick1 vs Qt Quick2
- Qt Quick1 use QGraphicsView and QPainter
- Qt Quick1 use QWindow and QOpenGL which involves QSceneGraph
Debug tools
Test
qmlscene
syntax
Import
- Import a directory
- A local directoy, all qml files, no js files.
- A local directoy with a qmldir file, types specified in the qmldir file, may have js files.
- A remote directoy with a qmldir file, types specified in the qmldir file.
- Import to a namespace, import a directory dont create a namespace by default, but you can create one with import dir as namespace
- Import a module/plugin
- Modules can be c++ plugins or QML documents.
- Module is defined and exported by a qmldir file.
- Module’s name is a namespace.
- Module may contains QML types, js resources and c++ plugins, all thie file must in the same directory
- Identified modules
- A module requires at leat one type registered to be considered valid.
- Provide type information to qtcretor with qmltypes file
- Import path
- Directories contain a module definition qmldir file.
- QML2_IMPORT_PATH can be set to a list of import path which will be used by any QML application
- QML_IMPORT_PATH qmake keyword, used in qtcreator
- QML_IMPORT_TRACE environment variable, if set to 1, import path will be recoreded in log.
Scope and name resolution
Type system
- Object Type
- Made from c++
- Made form qml
elements
// TODO create a online inherits diagram
-
Visual elements has a geometry on the screen, Item is the base.
-
non-visual elements provides some functionality.
-
- A seprate qml file
- A Component type in a qml file.
-
QtQuick
- Rectangle
- MouseArea
- Image
- Text
- Canvas
- Grid
- Column
- Row
-
- Layout
- GridLayout
- ColumnLayout
- RowLayout
- StackLayout
Note: A layout is responsible for its children’s geometry. You should therefore not specify width, height, x, y or any other properties that might influence those properties (such as anchors) on those items. Otherwise there would be a conflict of interest, and the result is undefined. This is also the case if the child item is a layout. Therefore, only layouts with no parent layout can have anchors.fill: parent.
-
- Action
- ActionGroup
- ApplicationWindow
- ButtonGroup
- Controls
- AbstractButton
- Button
- RoundButton
- ToolButton
- CheckBox
- DelayButton
- ItemDelegate
- CheckDelegate
- RadioDelegate
- SwipeDelegate
- SwitchDelegate
- MenuBarItem
- RadioButton
- Switch
- TabButton
- ScrollBar
- ScrollIndicator
- Button
- BusyIndicator
- Combobox
- Dial
- MenuSeparator
- Pane
- Frame
- GroupBox
- Page
- ScrollView
- ToolBar
- Frame
- PageIndicator
- ProgressBar
- RangeSlider
- Slider
- SpinBox
- SplitView
- StackView
- ToolSeparator
- Tumbler
- AbstractButton
- Container
- DialogButtonBox
- MenuBar
- SwipeView
- TabBar
- Label
- Popup
- Dialog
- ToolTip
- Menu
- Drawer
- Splithandle
- TextArea
- TextField
-
QtQuick.Window
user defined elements
-
Component
-
C++?
layout
QtQuick.Layouts
- Alignment
- Resizable
- Size constraints
- Spacing
More about item size.
-
Item
- width
- height
- implicitWidth, will be used if width is not specified
- implicitHeight, will be used if height is not specified.
-
Layouts
- Layout.minimumWidth
- Layout.prefferedHeight
- Layout.MaxmumWidth
-
If Layout.fillWidth/Height set to false, Layout.prefferedWidth/Height will be used; if set to true, width/height is between [min, max]
-
The implicitWidth/Height depends on its contents.
-
Image and Text has readonly implicitWidth/Height.
Flow
Controls
Control is the base type of all items in QtQuick.Control.
Manual/Fixed position
Anchor
Positioners
Layout
State and transtision
Model and views
model is datas,delegate is responsible visualization for every data, view is like a container to put those UIs.
|
|
Qt has predefined views and models, you can create any UI to serve as a delegate.Most of the time, you may want to create your own delegates and use predefined models and views.
Model
- role, can be accessed directly or through model.[role_name]
- modelData role
- index role
QML predefined model
ListModel
Use C++ Model
- QList<QObject*>, access with model.model.ModelDta[.property]
- QStringList, access with model.modelData
Using C++ Models with Qt Quick Views
Views
- ListView
- GridView
- PathView
Repeater
Repeater can be seen as a very basic view, it creates elements according to model and delegate.However, a repeater does’t arrange elements it created, and ofen used with Layout/Positioner to do the arrange job.
- delegate is the default property of repeater.
- The parent of the created elements is the repeater’s parent.
- Delegate of repeater must be a Item-based.
- All items in repeater have an attached property index
- model of repeater can be many types including Models in QML, different type expose different property to delegate.
- model object
- number
[Qt Widgets Model/View]
QAbstractItemModel is the interface of all models.
Interact with c++
- C++ -> QML: c++ code serve as model and do heavy logic
- Register c++ class/object to qml engine
- Create a plugin with c++
- QML -> C++: Load a qml file to c++ code.
- Load with QQmlComponent->load a qml file as a c++ object.
- Load with QQuickView/QQuickView.QQuickWidget is a convinient wrapper of QQuickWindow.
Overview of QML and C++ integration
Interacting with QML Objects from C++
Type conversion between QML and C++
Type conversion between QML and C++
Graphics View Framewok
Scene graph
Render flow
|
|
Important classes used
|
|
How to add custom node to scene graph
- Create a custom class subclassing
QQuickItem
- Set flag with
setFlag(ItemHasContents)
- Override
QQuickItem::updatePaintNode()
, useQSGNode::*ChildNode()
Frame graph
3D
Multi-threads
QML/Js engine is single thread? WorkerScript
Internals
QQuickItem(c++) Item(QML) QQmlEngine QQmlComponent QQmlContext Expose C++ OBject to to QML.
iterate qml in c++
Question to be answered
- A c++ type registerd to QML engine can not be created in c++ code?(video_hub code)
- How to know width and height of ListView?
Reference
Author tonghao
LastMod 2019-11-14