Sign up for the KDAB Newsletter
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Find what you need - explore our website and developer resources
19 May 2022
Do you remember to add qAsConst around your Qt containers when you iterate them? If not, there is a real risk that you are paying a price tag for them detaching just as you are about to start.
In this episode, Jesper has a chat with his coworker Peppe to better understand what qAsConst does, why and when it is needed, and how to work around issues with iterating containers that are the result of calling a function.
0:00 Introduction
0:57 implicit shared
3:16 the container might be copied
3:40 what does the for loop expands to?
5:04 What is the cure?
5:35 Solution: create a const copy of the contrainer.
7:00 Solution: Make the method const
7:40 Colution: use qAsConst
9:05 qAsConst and rvalues
10:30 But but but - nobody else has access...
12:45 Returning a const
15:05 Standard containers
15:46 a C++20 solution
17:04 Summary
The example code showcased in this video is available here: https://github.com/KDAB/kdabtv/tree/master/Qt-Widgets-and-more/qAsConst
All QML tips and tricks videos : https://www.youtube.com/playlist?list=PL6CJYn40gN6jWHP5krsQrVGyYtKh3A3be
All Qt Widgets and more videos:
https://www.youtube.com/playlist?list=PL6CJYn40gN6gf-G-o6syFwGrtq3kItEqI