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.
Some time ago, I wrote a post about integrating Qt's associative containers with the fancy new C++ features, range-based for loops with structured bindings.
That post inspired KDAB's own Giuseppe D'Angelo to add the asKeyValueRange member function to both QHash and QMap. Now it's possible to iterate over them with a simple range-based for loop, like so:
The second part of my previous post demonstrates how we can iterate over Qt SQL results using a range-based for loop as if it were an ordinary collection. In addition, it announces this second part of the post that shows you how to add the support for structured bindings into the mix.
Structured Bindings, Revisited
Recall that structured bindings allow us to decompose structures such as std::pair and QPair, so that we can give more intuitive names to the fields in those structures than the first and second names provided by both pair types:
auto[x, y]=mousePosition();
In the previous example, when the range-based for loop iterates over map.asKeyValueRange(), it takes each key-value pair from the map and assigns the name key to the first element of the pair and the name value to the second element.
Internally, the pair is stored in an invisible variable, and the names key and value just refer to the fields inside of that variable.
Out-of-the-box, structured bindings can also be used with arrays, tuple-like types, or, a bit less useful outside of generic programming, with ordinary user-defined structures:
It is also possible to make our own types decomposable with structured bindings, by making sure our types implement the so-called tuple protocol or, in other words, by making our types look like tuples.
Imagine we don't want the m_active member variable to be seen when using structured bindings on the previously defined Employee type. Instead, we just want to be able to bind m_name and m_team:
auto&[name, team_name]= employee;
In order to specify how our type should be decomposed with structured bindings, we need to define a few things:
into how many values an instance of our type can be decomposed;
the type of each of those values;
a getter for each of those values.
The first part of the tuple protocol is simple -- we need to specialize the std::tuple_size template for our type Employee. Since we only want to bind m_name and m_team, the size of our tuple-like type will be 2.
#include<utility>...namespace std {template<>structtuple_size<::Employee>{staticconstexpr std::size_t value =2;};}
The next step is to specialize the std::tuple_element template:
namespace std {template<>structtuple_element<0,::Employee>{// The type of m_nameusing type = QString;};template<>structtuple_element<1,::Employee>{// The type of m_teamusing type = QString;};}
The value we defined in the std::tuple_size specialization tells the compiler how many values it will get when it decomposes an instance of Employee, and the types we defined in the std::tuple_element specializations are the types of those values. In our case, both values are QStrings.
The last step is to create a get function template. It can be a member of Employee, but it can also be a free function (non-member) template.
It's worth noting that this implementation will not accept const objects and you'll need to provide a get implementation that takes a reference to a constEmployee, if you want to support those as well.
Decomposing QSqlRecord
Now that we know what we need to implement in order for our types to be usable with structured bindings, we can try to do it with QSqlResultIterator, which we implemented in part 1 of this blog post.
As a reminder, in the first part of the post, we implemented the QSqlResultIterator class that can be used to iterate over all results of a QSqlQuery. We also implemented operator[] on it, which allows us to access fields in a result.
We can use this to base the get function template on. To demonstrate that get doesn't need to be a free function, we will implement it as a member of QSqlResultIterator:
The remaining things that need to be implemented are the specializations of std::tuple_size and std::tuple_element.
Since all values in QSqlResult are QVariants, specializing std::tuple_element is trivial. For any index we're given, we just need to set type = QVariant:
namespace std {template<std::size_t Idx>structtuple_element<Idx, QSqlResultIterator>{using type = QVariant;};}
The std::tuple_size, on the other hand, is tricky. SQL queries are a runtime thing, and we need to know the number of fields in a record at compile time. This means that we need to allow the user to explicitly define the number of fields in a record when creating the QSqlResultIterator. One way to do it is to make QSqlResultIterator a class template with one std::size_t parameter:
We could even add a few static_asserts that would check that Idx is less than FieldCount everywhere.
Broken Range-based For Loop Use-case
When we added the FieldCount template parameter to QSqlResultIterator, we broke the use-case we had in the part 1 of this post. We now require FieldCount to be specified explicitly when an instance of QSqlResultIterator is created, and we are not creating it anywhere explicitly.
As a reminder, the QSqlResultIterator was instantiated by the range-based for loop which called begin on the QSqlQuery instance we passed to it:
for(auto result: query){// ...}
It happened behind the scenes and we cannot control how begin is called by the range-based for loop to be able to pass in FieldCount somehow.
Or, can we?
We can write a simple wrapper similar to what we did for asKeyValueRange. Then, instead of begin being defined for QSqlQuery directly, it would be defined for that wrapper, and it would be able to create QSqlResultIterator with the proper FieldCount value.
So far, we've implemented a range object that allows us to iterate over SQL results using a range-based for loop with structured bindings.
In the previous example, we get bindings active, name and team that all have QVariant type.
Can we improve the type-safety of this code, considering our almost always storing concrete types in a database and having to unwrap all QVariants, manually, is tedious and error prone?
Can we specify that the range object returns a bool and two QStrings in each iteration?
As usual in C++, the answer here is a resounding yes. The only thing that we need to do is replace all occurrences of the FieldCount parameter with a variadic pack of types, which will allow us to specify the exact types we expect to get in each resulting row of an SQL query.
In order avoid mixing these with the previously defined types, we'll add Typed to the names of classes we've created so far.
// We want to allow the user to specify the types// of the fields in an SQL rowtemplate<typename...Types>classQSqlResultTypedIterator{public:// The constructor and all the basic functions// we had in QSqlResultIterator remain unchangedQSqlResultTypedIterator(QSqlQuery& query):m_query(query){ m_query.next();} QSqlResultTypedIterator&operator++(){ m_query.next();return*this;}booloperator!=(QSqlResultSentinel sentinel)const{Q_UNUSED(sentinel);return m_query.isValid();} QSqlResultTypedIterator&operator*(){return*this;}// The only one that differs is the tuple-compatible// get member function. It can return different types// depending on the provided index.template<size_t Idx>autoget()const{using ResultType = std::tuple_element_t<Idx, std::tuple<Types...>>;// We can assert that the type stored inside of QVariant// is the type that we expect to be in it.Q_ASSERT(m_query.value(Idx).canView<ResultType>());// .value returns a QVariant. Then we call .value// on said variant to convert it to the desired type.return m_query.value(Idx).value<ResultType>();}private: QSqlQuery& m_query;};namespace std {// The tuple_size for QSqlResultTypedIterator is the// number of types inside of Types... which we can// easily get with sizeof...(Types)template<typename... Types>structtuple_size<QSqlResultTypedIterator<Types...>>:public integral_constant<size_t,sizeof...(Types)>{};// The simplest way to implement tuple_element on our type// is to just base it on the implementation of std::tuple itself.// When we are asked for tuple_element<Idx, QSqlResultTypedIterator<Types...>>,// we will just replace QSqlResultTypedIterator with std::tuple,// and return tuple_element<Idx, std::tuple<Types...>>template<std::size_t Idx,typename... Types>structtuple_element<Idx, QSqlResultTypedIterator <Types...>>: tuple_element<Idx, std::tuple <Types...>>{};}// The complex part was in the QSqlResultTypedIterator, and the// range object remains as simple as QSqlResultIterator was.// The only change is that FieldCount is replaced by Types... everywheretemplate<typename...Types>classQSqlResultTypedRange{public:QSqlResultTypedRange(QSqlQuery query):m_query(std::move(query)){} QSqlResultTypedIterator<Types...>begin(){return{ m_query };} QSqlResultSentinel end()const{return{};}private: QSqlQuery m_query;};
Wrap-up
This was a lengthy post to follow, and it had a lot of non-trivial code for something as simple as being able to write:
While this might not look like it is worth doing, remember that this is something you need to write only once and you will use it a lot, if you have an application that is SQL-heavy.
It will make your code more easy-to-read and as type-safe as possible when SQL is concerned, since the debug builds will assert that you are not trying to coerce values of one type into being something that they are not, when crossing the border between SQL and C++.
It is also easily extendible, to allow the user to skip the conversion from QVariant to a specific type by skipping the .value<ResultType>() part when the user-specified QVariant is the desired type, or to support using wrapper types such as std::optional when you have fields in an SQL table that can be NULL.
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.
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.