SlideShare a Scribd company logo
1 of 38
Download to read offline
© Integrated Computer Solutions, Inc. All Rights Reserved
Best Practices in Qt
Quick/QML Part 2
Justin Noel
Senior Consulting Engineer
ICS, Inc.
© Integrated Computer Solutions, Inc. All Rights Reserved
Agenda
• Creating New Items
• State and Transitions
• Dynamic Creation of Items
© Integrated Computer Solutions, Inc. All Rights Reserved
Creating New Items
© Integrated Computer Solutions, Inc. All Rights Reserved
Extending Items
• Any instance of a QML item is effectively a subclass
• Add member properties
• Add member functions
• Add signals
• There is no reason to add signals to an instance
© Integrated Computer Solutions, Inc. All Rights Reserved
Extending Items
Rectangle{
Text {
id: label
property int count: 1
text: “Count = ” + count
function incrementCount() {
count = count+1
}
}
Timer {
running: true
onTriggered: label.incrementCount()
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Property Types
• Properties can be almost any type
• Basic: int, real, string, date, time, point
• Copies are stored
• Any valid QML type: Rectangle, Text
• These are actually pointers
• The “var” type is equivalent to QVariant
• Use explicit types as much as you can
• They are faster
© Integrated Computer Solutions, Inc. All Rights Reserved
Property Types
Rectangle{
id: customItem
property int anInt: 1
property real aDouble: 50.5
property bool aBool: false
property string aString: “”
property var anything: 50.5
property list<point> points: [ Qt.point(0,0), Qt.point(100,100) ]
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Dividing Code Into
Components
• Often a devs to put too much code in one QML file
• Common issue for all programming languages
• QML makes it easy to componentize your code
• Component refers to an item that can be instanced
multiple times
© Integrated Computer Solutions, Inc. All Rights Reserved
Creating New Items
• Simply create a new .qml file
• Type is named after the filename
• Must begin with a capital letter
• Implement
• Properties
• Signals
• Functions
© Integrated Computer Solutions, Inc. All Rights Reserved
Inline Button Code
Rectangle{ // Main.qml
id: toplevel
color: “black”
Rectangle {
id: button
width: 100; height: 50
color: (mousArea.pressed) ? “red” : “blue”
Text {
text: “Click Me
}
MouseArea {
id: mouseArea
anchors.fill: parent
onClicked: toplevel.color = “white”
}
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Componentized Button
Rectangle{ // Main.qml
id: toplevel
color: “black”
Button {
text: “Click Me”
onClicked: toplevel.color = “white”
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Componentized Button
Rectangle{ // Button.qml
id: button
property alias text: label.text
signal clicked()
color: “blue”
width: 100; height: 50
Text {
id: label
anchors.centerIn: parent
}
MouseArea{
id: ma
anchors.fill: parent
onClicked: button.clicked()
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Alias Properties
• Proxies properties of child items
• Allows hiding implementation details
• Saves memory and binding recalculations
© Integrated Computer Solutions, Inc. All Rights Reserved
Property Scope
• Public Scope
• All public properties of the root item
• Custom properties defined on the root item
• Private Scope
• All child items and their properties
© Integrated Computer Solutions, Inc. All Rights Reserved
Public Members
Rectangle{ // Button.qml
id: button
property alias text: label.text
signal clicked()
color: “blue”
Text {
id: label
anchors.centerIn: parent
}
MouseArea{
id: ma
anchors.fill: parent
onClicked: button.clicked()
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Private Members
Rectangle{ // Button.qml
id: button
property alias text: label.text
signal clicked()
color: “blue”
Text {
id: label
anchors.centerIn: parent
}
MouseArea{
id: ma
anchors.fill: parent
onClicked: button.clicked()
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Private Properties
Rectangle { // Button.qml
id: button
property alias text: label.text
signal clicked()
QtObject {
id: internal
property int centerX: button.width()/2
}
Text {
x: internal.centerX
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Avoid Inheriting Public
Members
// Inherit from the basic Item type and hide everything else
Item { // Button.qml
id: button
property alias text: label.text
signal clicked()
Rectangle {
id: background
color: “blue”
}
Text {
id: label
anchors.centerIn: parent
}
...
}
© Integrated Computer Solutions, Inc. All Rights Reserved
States and Transitions
© Integrated Computer Solutions, Inc. All Rights Reserved
States
• State Machines can make your code “more
declarative”
• A basic state machine is built into every Item
• No parallel states or state history
© Integrated Computer Solutions, Inc. All Rights Reserved
States
• Every Item has a states property
• States contain
• Name
• When Clause
• List of PropertyChanges{} objects
© Integrated Computer Solutions, Inc. All Rights Reserved
Setting States
• Item can be set to a give state two ways
• 1) “state” property is set to the name of the State
• item.state = “Pressed”
• 2) The when clause of the State is true
• When clauses must be mutually exclusive
• They are evaluated in creation order
© Integrated Computer Solutions, Inc. All Rights Reserved
Button States
Item {
Rectangle { id: bkg; color: “blue” }
MouseArea { id: ma }
states: [
State {
name: “Pressed”
when: ma.pressed
PropertyChanges { target: bkg; color: “red” }
},
State {
name: “Disabled”
when: !(ma.enabled)
PropertyChanges { target: bkg; color: “grey” }
}
]
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Default State
• The initial bindings are the “Default State”
• The name of the default state is “”
• Default state is in effect when
• No when clauses are satisfied
• “state” property is set to “”
© Integrated Computer Solutions, Inc. All Rights Reserved
Properties When in a State
• The bindings of a QML document is defined as
• The default state bindings
• Overlaid with PropertyChanges from the current
state
• This will save you a ton of typing
• States do not need to be unwound
• Set common properties in the default state
• Avoids writing duplicate PropertyChanges
© Integrated Computer Solutions, Inc. All Rights Reserved
Transitions
• Run animations on a state change
• Control how properties will change
• Qt will automatically interpolate values
• Control in which order properties change
© Integrated Computer Solutions, Inc. All Rights Reserved
Transitions
[ ... ]
transitions: [
Transition {
from: “”; to: “Pressed”
PropertyAnimation { target: bkg
properties: “color”
duration: 500
},
Transition {
from: “*”; to: “Disabled”
PropertyAnimation { target: bkg
properties: “color”
duration: 250
}
]
[ ... ]
© Integrated Computer Solutions, Inc. All Rights Reserved
Transition Defaults
• Transition{} defaults to
• from: “*”; to: “*”
• That Transition will apply to all state changes
• PropertyAnimation
• When a target is not specified
• That animation will apply to all items
© Integrated Computer Solutions, Inc. All Rights Reserved
Button Transition
Item {
Rectangle { id: bkg; color: “blue” }
MouseArea { id: ma }
states: [
State { name: “Pressed”; when: ma.pressed
PropertyChanges { target: bkg; color: “red” }
},
State { name: “Disabled”; when: !(ma.enabled)
PropertyChanges { target: bkg; color: “grey” }
}
]
transitions: [
Transition {
PropertyAnimation {
properties: “color”
duration: 500
}
}
]
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Dynamic Creation of Items
© Integrated Computer Solutions, Inc. All Rights Reserved
Creating Items Dynamically
• Procedural Way
• Component createObject(parent, bindings) function
• Declarative Way
• Loader Item
• Repeater Item
• ListView / GridView Items
© Integrated Computer Solutions, Inc. All Rights Reserved
Procedural Creation
Item {
id: screen
property SettingDialog dialog: undefined
Button {
text: “Settings...”
onClicked: {
var component = Qt.createComponent(“SettingsDialog.qml”)
screen.dialog = component.createObject(screen,
{ “anchors.centerIn”: “screen” })
screen.dialog.close.connect(screen.destroySettingsDialog)
}
function destroySettingsDialog()
{
screen.dialog.destroy()
screen.dialog = undefined
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Procedural / Declarative
Creation
Item {
id: screen
Button {
text: “Settings...”
onClicked: screen.dialog = dialogComponent.createObject(screen)
}
Component {
id: dialogComponent
SettingsDialog {
anchors.centerIn: parent
onClose: destroy()
}
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Declarative Creation
Item {
Button {
text: “Settings...”
onClicked: loader.sourceComponent = dialogComponent
Loader {
id: loader
anchors.fill: parent
}
Component {
id: dialogComponent
SettingsDialog {
anchors.centerIn: parent
onClose: loader.sourceComponent = undefined
}
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Creating Multiple Items
Item {
width: 400; height: 400
color: “black”
Grid {
x: 5; y:5
rows: 5; columns: 5
Repeater {
model: 24
Rectangle {
width: 70; height: 70
color: “lightgreen”
}
}
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Creating Multiple Items
Item {
width: 400; height: 400
color: “black”
Grid {
x: 5; y:5
rows: 5; columns: 5
Repeater {
model: 24
Rectangle {
width: 70; height: 70
color: “lightgreen”
Text {
anchors.centerIn: parent
text: index
}
}
}
}
}
© Integrated Computer Solutions, Inc. All Rights Reserved
Repeater
• Repeaters can use all type of data models
• ListModel
• JSON Data
• property list<type>
• QList<QObject*>
• QAbstractItemModel
• Model data is accessed via attached properties
© Integrated Computer Solutions, Inc. All Rights Reserved
Upcoming Webinars
• QML by Design, July 13
• This webinar will touch on the benefits of having designers
trained in the same framework the programmers are using.
• Best Practices in Qt Quick/QML - Part 3, July 27
• C++ / QML Integration
• Reusing Existing C++ code

More Related Content

Similar to Best Practices in Qt Quick/QML - Part 2

Fun with QML
Fun with QMLFun with QML
Fun with QMLICS
 
Porting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarPorting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarICS
 
Porting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarPorting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarJanel Heilbrunn
 
Best Practices in Qt Quick/QML - Part 3
Best Practices in Qt Quick/QML - Part 3Best Practices in Qt Quick/QML - Part 3
Best Practices in Qt Quick/QML - Part 3ICS
 
Best Practices in Qt Quick/QML - Part 4
Best Practices in Qt Quick/QML - Part 4Best Practices in Qt Quick/QML - Part 4
Best Practices in Qt Quick/QML - Part 4ICS
 
Session iii(server controls)
Session iii(server controls)Session iii(server controls)
Session iii(server controls)Shrijan Tiwari
 
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろうUnity Technologies Japan K.K.
 
Magic of web components
Magic of web componentsMagic of web components
Magic of web componentsHYS Enterprise
 
Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2NokiaAppForum
 
Ubuntu app development
Ubuntu app development Ubuntu app development
Ubuntu app development Xiaoguo Liu
 
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...Rebekka Aalbers-de Jong
 
Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)Thinkful
 
Mobile services on windows azure (part2)
Mobile services on windows azure (part2)Mobile services on windows azure (part2)
Mobile services on windows azure (part2)Radu Vunvulea
 
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...goodfriday
 
El camino a las Cloud Native Apps - Introduction
El camino a las Cloud Native Apps - IntroductionEl camino a las Cloud Native Apps - Introduction
El camino a las Cloud Native Apps - IntroductionPlain Concepts
 
How to Program SmartThings
How to Program SmartThingsHow to Program SmartThings
How to Program SmartThingsJanet Huang
 
Evan Schultz - Angular Summit - 2016
Evan Schultz - Angular Summit - 2016Evan Schultz - Angular Summit - 2016
Evan Schultz - Angular Summit - 2016Evan Schultz
 

Similar to Best Practices in Qt Quick/QML - Part 2 (20)

Fun with QML
Fun with QMLFun with QML
Fun with QML
 
Porting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarPorting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - Webinar
 
Porting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - WebinarPorting Motif Applications to Qt - Webinar
Porting Motif Applications to Qt - Webinar
 
Best Practices in Qt Quick/QML - Part 3
Best Practices in Qt Quick/QML - Part 3Best Practices in Qt Quick/QML - Part 3
Best Practices in Qt Quick/QML - Part 3
 
Best Practices in Qt Quick/QML - Part 4
Best Practices in Qt Quick/QML - Part 4Best Practices in Qt Quick/QML - Part 4
Best Practices in Qt Quick/QML - Part 4
 
Javascript
JavascriptJavascript
Javascript
 
Session iii(server controls)
Session iii(server controls)Session iii(server controls)
Session iii(server controls)
 
UI Programming with Qt-Quick and QML
UI Programming with Qt-Quick and QMLUI Programming with Qt-Quick and QML
UI Programming with Qt-Quick and QML
 
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
【Unite 2017 Tokyo】ScriptableObjectを使ってプログラマーもアーティストも幸せになろう
 
Magic of web components
Magic of web componentsMagic of web components
Magic of web components
 
Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 2
 
Ubuntu app development
Ubuntu app development Ubuntu app development
Ubuntu app development
 
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
 
Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)Build a game with javascript (may 21 atlanta)
Build a game with javascript (may 21 atlanta)
 
Mobile services on windows azure (part2)
Mobile services on windows azure (part2)Mobile services on windows azure (part2)
Mobile services on windows azure (part2)
 
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
 
WOdka
WOdkaWOdka
WOdka
 
El camino a las Cloud Native Apps - Introduction
El camino a las Cloud Native Apps - IntroductionEl camino a las Cloud Native Apps - Introduction
El camino a las Cloud Native Apps - Introduction
 
How to Program SmartThings
How to Program SmartThingsHow to Program SmartThings
How to Program SmartThings
 
Evan Schultz - Angular Summit - 2016
Evan Schultz - Angular Summit - 2016Evan Schultz - Angular Summit - 2016
Evan Schultz - Angular Summit - 2016
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 

Recently uploaded (20)

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 

Best Practices in Qt Quick/QML - Part 2

  • 1. © Integrated Computer Solutions, Inc. All Rights Reserved Best Practices in Qt Quick/QML Part 2 Justin Noel Senior Consulting Engineer ICS, Inc.
  • 2. © Integrated Computer Solutions, Inc. All Rights Reserved Agenda • Creating New Items • State and Transitions • Dynamic Creation of Items
  • 3. © Integrated Computer Solutions, Inc. All Rights Reserved Creating New Items
  • 4. © Integrated Computer Solutions, Inc. All Rights Reserved Extending Items • Any instance of a QML item is effectively a subclass • Add member properties • Add member functions • Add signals • There is no reason to add signals to an instance
  • 5. © Integrated Computer Solutions, Inc. All Rights Reserved Extending Items Rectangle{ Text { id: label property int count: 1 text: “Count = ” + count function incrementCount() { count = count+1 } } Timer { running: true onTriggered: label.incrementCount() } }
  • 6. © Integrated Computer Solutions, Inc. All Rights Reserved Property Types • Properties can be almost any type • Basic: int, real, string, date, time, point • Copies are stored • Any valid QML type: Rectangle, Text • These are actually pointers • The “var” type is equivalent to QVariant • Use explicit types as much as you can • They are faster
  • 7. © Integrated Computer Solutions, Inc. All Rights Reserved Property Types Rectangle{ id: customItem property int anInt: 1 property real aDouble: 50.5 property bool aBool: false property string aString: “” property var anything: 50.5 property list<point> points: [ Qt.point(0,0), Qt.point(100,100) ] }
  • 8. © Integrated Computer Solutions, Inc. All Rights Reserved Dividing Code Into Components • Often a devs to put too much code in one QML file • Common issue for all programming languages • QML makes it easy to componentize your code • Component refers to an item that can be instanced multiple times
  • 9. © Integrated Computer Solutions, Inc. All Rights Reserved Creating New Items • Simply create a new .qml file • Type is named after the filename • Must begin with a capital letter • Implement • Properties • Signals • Functions
  • 10. © Integrated Computer Solutions, Inc. All Rights Reserved Inline Button Code Rectangle{ // Main.qml id: toplevel color: “black” Rectangle { id: button width: 100; height: 50 color: (mousArea.pressed) ? “red” : “blue” Text { text: “Click Me } MouseArea { id: mouseArea anchors.fill: parent onClicked: toplevel.color = “white” } } }
  • 11. © Integrated Computer Solutions, Inc. All Rights Reserved Componentized Button Rectangle{ // Main.qml id: toplevel color: “black” Button { text: “Click Me” onClicked: toplevel.color = “white” } }
  • 12. © Integrated Computer Solutions, Inc. All Rights Reserved Componentized Button Rectangle{ // Button.qml id: button property alias text: label.text signal clicked() color: “blue” width: 100; height: 50 Text { id: label anchors.centerIn: parent } MouseArea{ id: ma anchors.fill: parent onClicked: button.clicked() } }
  • 13. © Integrated Computer Solutions, Inc. All Rights Reserved Alias Properties • Proxies properties of child items • Allows hiding implementation details • Saves memory and binding recalculations
  • 14. © Integrated Computer Solutions, Inc. All Rights Reserved Property Scope • Public Scope • All public properties of the root item • Custom properties defined on the root item • Private Scope • All child items and their properties
  • 15. © Integrated Computer Solutions, Inc. All Rights Reserved Public Members Rectangle{ // Button.qml id: button property alias text: label.text signal clicked() color: “blue” Text { id: label anchors.centerIn: parent } MouseArea{ id: ma anchors.fill: parent onClicked: button.clicked() } }
  • 16. © Integrated Computer Solutions, Inc. All Rights Reserved Private Members Rectangle{ // Button.qml id: button property alias text: label.text signal clicked() color: “blue” Text { id: label anchors.centerIn: parent } MouseArea{ id: ma anchors.fill: parent onClicked: button.clicked() } }
  • 17. © Integrated Computer Solutions, Inc. All Rights Reserved Private Properties Rectangle { // Button.qml id: button property alias text: label.text signal clicked() QtObject { id: internal property int centerX: button.width()/2 } Text { x: internal.centerX } }
  • 18. © Integrated Computer Solutions, Inc. All Rights Reserved Avoid Inheriting Public Members // Inherit from the basic Item type and hide everything else Item { // Button.qml id: button property alias text: label.text signal clicked() Rectangle { id: background color: “blue” } Text { id: label anchors.centerIn: parent } ... }
  • 19. © Integrated Computer Solutions, Inc. All Rights Reserved States and Transitions
  • 20. © Integrated Computer Solutions, Inc. All Rights Reserved States • State Machines can make your code “more declarative” • A basic state machine is built into every Item • No parallel states or state history
  • 21. © Integrated Computer Solutions, Inc. All Rights Reserved States • Every Item has a states property • States contain • Name • When Clause • List of PropertyChanges{} objects
  • 22. © Integrated Computer Solutions, Inc. All Rights Reserved Setting States • Item can be set to a give state two ways • 1) “state” property is set to the name of the State • item.state = “Pressed” • 2) The when clause of the State is true • When clauses must be mutually exclusive • They are evaluated in creation order
  • 23. © Integrated Computer Solutions, Inc. All Rights Reserved Button States Item { Rectangle { id: bkg; color: “blue” } MouseArea { id: ma } states: [ State { name: “Pressed” when: ma.pressed PropertyChanges { target: bkg; color: “red” } }, State { name: “Disabled” when: !(ma.enabled) PropertyChanges { target: bkg; color: “grey” } } ] }
  • 24. © Integrated Computer Solutions, Inc. All Rights Reserved Default State • The initial bindings are the “Default State” • The name of the default state is “” • Default state is in effect when • No when clauses are satisfied • “state” property is set to “”
  • 25. © Integrated Computer Solutions, Inc. All Rights Reserved Properties When in a State • The bindings of a QML document is defined as • The default state bindings • Overlaid with PropertyChanges from the current state • This will save you a ton of typing • States do not need to be unwound • Set common properties in the default state • Avoids writing duplicate PropertyChanges
  • 26. © Integrated Computer Solutions, Inc. All Rights Reserved Transitions • Run animations on a state change • Control how properties will change • Qt will automatically interpolate values • Control in which order properties change
  • 27. © Integrated Computer Solutions, Inc. All Rights Reserved Transitions [ ... ] transitions: [ Transition { from: “”; to: “Pressed” PropertyAnimation { target: bkg properties: “color” duration: 500 }, Transition { from: “*”; to: “Disabled” PropertyAnimation { target: bkg properties: “color” duration: 250 } ] [ ... ]
  • 28. © Integrated Computer Solutions, Inc. All Rights Reserved Transition Defaults • Transition{} defaults to • from: “*”; to: “*” • That Transition will apply to all state changes • PropertyAnimation • When a target is not specified • That animation will apply to all items
  • 29. © Integrated Computer Solutions, Inc. All Rights Reserved Button Transition Item { Rectangle { id: bkg; color: “blue” } MouseArea { id: ma } states: [ State { name: “Pressed”; when: ma.pressed PropertyChanges { target: bkg; color: “red” } }, State { name: “Disabled”; when: !(ma.enabled) PropertyChanges { target: bkg; color: “grey” } } ] transitions: [ Transition { PropertyAnimation { properties: “color” duration: 500 } } ] }
  • 30. © Integrated Computer Solutions, Inc. All Rights Reserved Dynamic Creation of Items
  • 31. © Integrated Computer Solutions, Inc. All Rights Reserved Creating Items Dynamically • Procedural Way • Component createObject(parent, bindings) function • Declarative Way • Loader Item • Repeater Item • ListView / GridView Items
  • 32. © Integrated Computer Solutions, Inc. All Rights Reserved Procedural Creation Item { id: screen property SettingDialog dialog: undefined Button { text: “Settings...” onClicked: { var component = Qt.createComponent(“SettingsDialog.qml”) screen.dialog = component.createObject(screen, { “anchors.centerIn”: “screen” }) screen.dialog.close.connect(screen.destroySettingsDialog) } function destroySettingsDialog() { screen.dialog.destroy() screen.dialog = undefined } }
  • 33. © Integrated Computer Solutions, Inc. All Rights Reserved Procedural / Declarative Creation Item { id: screen Button { text: “Settings...” onClicked: screen.dialog = dialogComponent.createObject(screen) } Component { id: dialogComponent SettingsDialog { anchors.centerIn: parent onClose: destroy() } } }
  • 34. © Integrated Computer Solutions, Inc. All Rights Reserved Declarative Creation Item { Button { text: “Settings...” onClicked: loader.sourceComponent = dialogComponent Loader { id: loader anchors.fill: parent } Component { id: dialogComponent SettingsDialog { anchors.centerIn: parent onClose: loader.sourceComponent = undefined } } }
  • 35. © Integrated Computer Solutions, Inc. All Rights Reserved Creating Multiple Items Item { width: 400; height: 400 color: “black” Grid { x: 5; y:5 rows: 5; columns: 5 Repeater { model: 24 Rectangle { width: 70; height: 70 color: “lightgreen” } } } }
  • 36. © Integrated Computer Solutions, Inc. All Rights Reserved Creating Multiple Items Item { width: 400; height: 400 color: “black” Grid { x: 5; y:5 rows: 5; columns: 5 Repeater { model: 24 Rectangle { width: 70; height: 70 color: “lightgreen” Text { anchors.centerIn: parent text: index } } } } }
  • 37. © Integrated Computer Solutions, Inc. All Rights Reserved Repeater • Repeaters can use all type of data models • ListModel • JSON Data • property list<type> • QList<QObject*> • QAbstractItemModel • Model data is accessed via attached properties
  • 38. © Integrated Computer Solutions, Inc. All Rights Reserved Upcoming Webinars • QML by Design, July 13 • This webinar will touch on the benefits of having designers trained in the same framework the programmers are using. • Best Practices in Qt Quick/QML - Part 3, July 27 • C++ / QML Integration • Reusing Existing C++ code