Trusted Software Excellence across Desktop and Embedded
Take a glance at the areas of expertise where KDAB excels ranging from swift troubleshooting, ongoing consulting and training to multi-year, large-scale software development projects.
Find out why customers from innovative industries rely on our extensive expertise, including Medical, Biotech, Science, Renewable Energy, Transportation, Mobility, Aviation, Automation, Electronics, Agriculture and Defense.
High-quality Embedded Engineering across the Stack
To successfully develop an embedded device that meets your expectations regarding quality, budget and time to market, all parts of the project need to fit perfectly together.
Learn more about KDAB's expertise in embedded software development.
Where the capabilities of modern mobile devices or web browsers fall short, KDAB engineers help you expertly architect and build high-functioning desktop and workstation applications.
Extensible, Safety-compliant Software for the Medical Sector
Create intelligent, patient-focused medical software and devices and stay ahead with technology that adapts to your needs.
KDAB offers you expertise in developing a broad spectrum of clinical and home-healthcare devices, including but not limited to, internal imaging systems, robotic surgery devices, ventilators and non-invasive monitoring systems.
Building digital dashboards and cockpits with fluid animations and gesture-controlled touchscreens is a big challenge.
In over two decades of developing intricate UI solutions for cars, trucks, tractors, scooters, ships, airplanes and more, the KDAB team has gained market leading expertise in this realm.
Build on Advanced Expertise when creating Modern UIs
KDAB assists you in the creation of user-friendly interfaces designed specifically for industrial process control, manufacturing, and fabrication.
Our specialties encompass the custom design and development of HMIs, enabling product accessibility from embedded systems, remote desktops, and mobile devices on the move.
Legacy software is a growing but often ignored problem across all industries. KDAB helps you elevate your aging code base to meet the dynamic needs of the future.
Whether you want to migrate from an old to a modern GUI toolkit, update to a more recent version, or modernize your code base, you can rely on over 25 years of modernization experience.
KDAB offers a wide range of services to address your software needs including consulting, development, workshops and training tailored to your requirements.
Our expertise spans cross-platform desktop, embedded and 3D application development, using the proven technologies for the job.
When working with KDAB, the first-ever Qt consultancy, you benefit from a deep understanding of Qt internals, that allows us to provide effective solutions, irrespective of the depth or scale of your Qt project.
Qt Services include developing applications, building runtimes, mixing native and web technologies, solving performance issues, and porting problems.
KDAB helps create commercial, scientific or industrial desktop applications from scratch, or update its code or framework to benefit from modern features.
Discover clean, efficient solutions that precisely meet your requirements.
Boost your team's programming skills with in-depth, constantly updated, hands-on training courses delivered by active software engineers who love to teach and share their knowledge.
Our courses cover Modern C++, Qt/QML, Rust, 3D programming, Debugging, Profiling and more.
The collective expertise of KDAB's engineering team is at your disposal to help you choose the software stack for your project or master domain-specific challenges.
Our particular focus is on software technologies you use for cross-platform applications or for embedded devices.
Since 1999, KDAB has been the largest independent Qt consultancy worldwide and today is a Qt Platinum partner. Our experts can help you with any aspect of software development with Qt and QML.
KDAB specializes in Modern C++ development, with a focus on desktop applications, GUI, embedded software, and operating systems.
Our experts are industry-recognized contributors and trainers, leveraging C++'s power and relevance across these domains to deliver high-quality software solutions.
KDAB can guide you incorporating Rust into your project, from as overlapping element to your existing C++ codebase to a complete replacement of your legacy code.
Unique Expertise for Desktop and Embedded Platforms
Whether you are using Linux, Windows, MacOS, Android, iOS or real-time OS, KDAB helps you create performance optimized applications on your preferred platform.
If you are planning to create projects with Slint, a lightweight alternative to standard GUI frameworks especially on low-end hardware, you can rely on the expertise of KDAB being one of the earliest adopters and official service partner of Slint.
KDAB has deep expertise in embedded systems, which coupled with Flutter proficiency, allows us to provide comprehensive support throughout the software development lifecycle.
Our engineers are constantly contributing to the Flutter ecosystem, for example by developing flutter-pi, one of the most used embedders.
KDAB invests significant time in exploring new software technologies to maintain its position as software authority. Benefit from this research and incorporate it eventually into your own project.
Start here to browse infos on the KDAB website(s) and take advantage of useful developer resources like blogs, publications and videos about Qt, C++, Rust, 3D technologies like OpenGL and Vulkan, the KDAB developer tools and more.
The KDAB Youtube channel has become a go-to source for developers looking for high-quality tutorial and information material around software development with Qt/QML, C++, Rust and other technologies.
Click to navigate the all KDAB videos directly on this website.
In over 25 years KDAB has served hundreds of customers from various industries, many of them having become long-term customers who value our unique expertise and dedication.
Learn more about KDAB as a company, understand why we are considered a trusted partner by many and explore project examples in which we have proven to be the right supplier.
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications.
Read more about the history, the values, the team and the founder of the company.
When working with KDAB you can expect quality software and the desired business outcomes thanks to decades of experience gathered in hundreds of projects of different sizes in various industries.
Have a look at selected examples where KDAB has helped customers to succeed with their projects.
KDAB is committed to developing high-quality and high-performance software, and helping other developers deliver to the same high standards.
We create software with pride to improve your engineering and your business, making your products more resilient and maintainable with better performance.
KDAB has been the first certified Qt consulting and software development company in the world, and continues to deliver quality processes that meet or exceed the highest expectations.
In KDAB we value practical software development experience and skills higher than academic degrees. We strive to ensure equal treatment of all our employees regardless of age, ethnicity, gender, sexual orientation, nationality.
Interested? Read more about working at KDAB and how to apply for a job in software engineering or business administration.
Qt has a long history. The first stable version was released before the first version of C++ was standardized and long before the different C++ compiler vendors started shipping usable implementations of the C++ standard library. Because of this, Qt often followed (and still follows) some design idioms that feel unnatural to the usual C++ developer.
This can have some downsides for the Qt ecosystem. The evolution of the C++ programming language which sped up quite a bit in the last decade often brings improvements which don't fit well with the Qt philosophy. In this blog, I offer some ways to work with this.
Range-based for loops
C++11 brought us the range-based for loop which allows easy iteration over any iterable sequence, including common containers such as std::vector, std::map, etc.
for(auto name: names){// ...}
When you write code like the above, the compiler (C++11) sees the following:
It takes the begin and end iterators of the names collection, and iterates from one to the other.
Rather simple, but it is a very nice syntactical improvement to the language -- it is a significant reduction of boilerplate code and much easier on the eyes.
But, it was not designed with Qt collection classes in mind. You can check out the "Goodbye, Q_FOREACH" blog post by Marc for one example where the range-based for loop collided with the Qt design principles, most notably with the copy-on-write feature because of which we got the qAsConst helper function.
Structured bindings
Another new feature, this one from C++17, are the structured bindings.
They add syntactic sugar for decomposing structures. For example, if we have a function that returns a pair of values (std::pair or a QPair) we can declare variables that refer to the values inside of the pair instead of having to access them through .first and .second.
auto[x, y]=mousePosition();
This can significantly improve code readability. In the previous example, the names x and y communicate the meaning much better than .first and .second would.
This is even more the case if we use a function like QMultiMap::equal_range which returns a pair of iterators delimiting the range of values that are stored under a specified key in a QMap. Seeing participants.equal_range("kdab").second would look like we are accessing the second value in the map that is stored under the "kdab" key. If we used structured bindings, we could have sane variable names:
Structured bindings can lead to quite succulent code when combined with the range-based for loops for iteration over STL-compliant map-like containers:
for(auto[key, value]: stdMap){// ...}
Each key-value pair in stdMap, will be stored in an invisible (unnamed) variable, and key and value will be references to its inner elements.
Sidebar: don't be fooled by decltype(key) telling you key is a normal value, for all intents and purposes, it is a reference.
QHash, QMap and STL compatibility
Sadly, the previous code snippet can not work with Qt's associative containers because the API is not compatible with that of std::map and similar containers.The problem is in the API of the iterators for QHash and QMap. Instead of returning a key-value pair when you dereference the iterator, it returns only the value. This means that the range-based for loop can only iterate over the values stored inside of the collection, it can not (by default) access the keys:
for(auto value: map){// ...}
When we use the QHash and QMap iterators directly, we can access the key with .key() and the value with .value() but this does not help us with the range-based for loop. As we've seen, it is just a syntactic sugar which expands to an ordinary for loop that iterates over a collection automatically dereferencing (calling operator*()) the iterator in each iteration.
So, as far as the range-based for loop is concerned, the key and value member functions do not even exist.
This deficiency of the Qt's associative containers' API has been noticed, and QHash and QMap provide us with alternative, more STL-like, iterators since Qt 5.10 -- the key_value_iterator and the const_key_value_iterator. When dereferenced, these iterators return a std::pair containing both the key and the value. To quote the Qt documentation:
The QMap::key_value_iterator typedef provides an STL-style iterator for QMap and QMultiMap. QMap::key_value_iterator is essentially the same as QMap::iterator with the difference that operator*() returns a key/value pair instead of a value.
This means that we can use the structured bindings with them:
auto[key, value]=*map.keyValueBegin();
But we still cannot use this with the range-based for loop to iterate over all key-value pairs in the collection. The range-based for loop will call .begin() and .end(), and not .keyValueBegin() and .keyValueEnd().
This is easily remedied by creating a simple wrapper over QHash and QMap that will rename.keyValueBegin() and .keyValueEnd() to .begin() and .end() like so:
template<typenameT>classasKeyValueRange{public:asKeyValueRange(T &data): m_data{data}{}autobegin(){return m_data.keyValueBegin();}autoend(){return m_data.keyValueEnd();}private: T &m_data;};
Instead of passing a Qt associative container directly to the range-based for loop, we just need to pass it wrapped inside of the asKeyValueRange class, and we will finally be able to use the idiomatic C++17 way of iterating over key-value pairs with QHash and QMap:
It is important to note that this is a somewhat simplified implementation of the asKeyValueRange wrapper that takes the reference to the original QHash/QMap, so it might lead to a dangling reference if used improperly.
Iteration over Qt SQL results
Another part of Qt that hasn't been designed with common C++ idioms in mind is the Qt SQL module. Its API is designed as if it were a Java library and not a C++ one. Instead of having normal iterators over the results in the QSqlQuery, we need to use the .next() member function to fetch each result. The .next() member function returns a Boolean value denoting if there is another result, and the result itself is accessible through other QSqlQuery member functions.
Apart from the API, iteration over the QSqlQuery results has another difference to usual C++ iterators. The QSqlQuery does not need an end iterator to know whether there is another result available. If .next() or .isValid() return false, this means we have reached the end of the result set.
Having the iterator itself know when it has reached the end of the collection it belongs to is uncommon in STL, but it still is present. The input stream iterator (std::istream_iterator) is one such beast. We cannot know in advance where the end of an input stream is, we can just read values from the stream until the read fails. We have reached the end when the read failed. Similar to to the QSqlQuery -- we have reached the end when .next() fails -- when it returns false.
In these cases, the end iterator is only needed to fulfil the STL iterator API. It is just a dummy object (usually called a sentinel) that will trigger the validity check on the main iterator when it != end is evaluated.
C++17 brought a small change to the range-based for loop meant for this exact use-case. When C++17 compiler sees the range-based for loop from the first code snippet we had, it will convert it to the following code:
auto __begin =begin(names);auto __end =end(names);
While these two snippets look mostly the same, there is an important difference -- in the first snippet, both __begin and __end need to have the same type whereas they can be different in the second.
This allows us to create a begin function that returns a powerful iterator that knows when it has reached the end of a collection, while the end function returns a dummy value -- a sentinel.
The only thing that remains is for us to decide what the dereference operator should do.
Since each record in the result can contain several values, it would be nice if we provided the operator[] to access those values. Therefore, when we dereference the QSqlResultIterator, we want to get a type that has operator[] defined on it which returns values from the current record.
We have several options on how to do this. The simplest, though not the cleanest, is to have the dereference operator return the reference to the iterator itself, and then define the operator[] in the iterator class.
If we weren't interested in having the operator[] to access the values, then we could just return the current QSqlRecord with m_query.record() from the operator*().
While this is not a complete iterator implementation (we are missing iterator type traits and similar), it is sufficient for us to be able to use it with a range-based for loop:
We'll see what we need to do in order to be able to use structured bindings with Qt SQL in the next part of this post.
About KDAB
Trusted software excellence across embedded and desktop platforms
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications. In addition to being leading experts in Qt, C++ and 3D technologies for over two decades, KDAB provides deep expertise across the stack, including Linux, Rust and modern UI frameworks. With 100+ employees from 20 countries and offices in Sweden, Germany, USA, France and UK, we serve clients around the world.
Are you planning on adding such features to upstream Qt?
20 - Apr - 2020
Ivan Čukić
I have no plans to do it in the near future because these are (or can be) simple header-only classes. Maybe for Qt 6 at some point.
19 - Apr - 2020
Pierre Clouthier
Has not been a problem for me. Long live Qt!
20 - Apr - 2020
Ivan Čukić
Not a problem for me as well - more of an annoyance.
Long live Qt! :)
14 - Apr - 2022
Hamish M
I love the idea of the SQL iterator. Is there a completed solution available anywhere? (Is there a part 2 to this article?)
I'd like to try using QtConcurrent::map() over the SQL results, but the QtConcurrent methods all require the begin and end of the sequence to have the same type.
14 - Apr - 2022
Ivan Čukić
Thanks for reminding me. I need to write that one soon. :)
Though, I don't think concurrent access to SQL results is going to be a good idea - the results are not in-memory for them to be random access and many SQL engines are optimized for sequential result access, not a random access. Copying them into a std::vector/QVector would be the better approach (even if you only copy 10k results at a time and concurrently map them).
12 - Oct - 2022
Adaita
Will not this work?
for (auto &[key, value]: map.toStdMap()) {
// ...
}
14 - Oct - 2022
Ivan Čukić
Sure, but that is very inefficient as it copies the whole QMap into a temporary std::map just to be able to iterate over the collection.
Also, changing the value from inside the for loop would change the value stored in said temporary std::map instance, not in the original QMap.
19 - Oct - 2023
David Faure
UPDATE: since Qt 6.4, QMap and QHash offer asKeyValueRange() for this purpose. This was contributed by KDAB (Giuseppe D'Angelo). Even easier :-)
Ivan Čukić
Senior Software Engineer
Author of "Functional Programming in C++" (Manning), KDAB and KDE developer, C++ trainer and consultant.
Our hands-on Modern C++ training courses are designed to quickly familiarize newcomers with the language. They also update professional C++ developers on the latest changes in the language and standard library introduced in recent C++ editions.
9 Comments
19 - Apr - 2020
Richi
Are you planning on adding such features to upstream Qt?
20 - Apr - 2020
Ivan Čukić
I have no plans to do it in the near future because these are (or can be) simple header-only classes. Maybe for Qt 6 at some point.
19 - Apr - 2020
Pierre Clouthier
Has not been a problem for me. Long live Qt!
20 - Apr - 2020
Ivan Čukić
Not a problem for me as well - more of an annoyance.
Long live Qt! :)
14 - Apr - 2022
Hamish M
I love the idea of the SQL iterator. Is there a completed solution available anywhere? (Is there a part 2 to this article?)
I'd like to try using QtConcurrent::map() over the SQL results, but the QtConcurrent methods all require the begin and end of the sequence to have the same type.
14 - Apr - 2022
Ivan Čukić
Thanks for reminding me. I need to write that one soon. :)
Though, I don't think concurrent access to SQL results is going to be a good idea - the results are not in-memory for them to be random access and many SQL engines are optimized for sequential result access, not a random access. Copying them into a std::vector/QVector would be the better approach (even if you only copy 10k results at a time and concurrently map them).
12 - Oct - 2022
Adaita
Will not this work?
for (auto &[key, value]: map.toStdMap()) { // ... }
14 - Oct - 2022
Ivan Čukić
Sure, but that is very inefficient as it copies the whole
QMap
into a temporarystd::map
just to be able to iterate over the collection.Also, changing the
value
from inside the for loop would change the value stored in said temporarystd::map
instance, not in the originalQMap
.19 - Oct - 2023
David Faure
UPDATE: since Qt 6.4, QMap and QHash offer asKeyValueRange() for this purpose. This was contributed by KDAB (Giuseppe D'Angelo). Even easier :-)