Discover how to correctly read custom struct values from `QSettings` in Qt using C+ + . This guide breaks down the solution to common problems encountered with `QVariant`.
---
This video is based on the question https://stackoverflow.com/q/72639072/ asked by the user 'Jager' ( https://stackoverflow.com/u/17819464/ ) and on the answer https://stackoverflow.com/a/72663388/ provided by the user 'Jager' ( https://stackoverflow.com/u/17819464/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Can't get correct value with QSettings and custom type
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding the Problem: Issues with QSettings and Custom Data Types
When working with QSettings in C+ + , particularly with Qt, you might encounter a frustrating situation where reading custom struct values results in default values, such as {0, 0}. This problem often stems from the complex behavior of QVariant and Qt's type management system. If you’re new to Qt, these issues can be particularly confusing. Let's dive deeper and see how to solve it!
The Setup: Custom Struct Definition
In this situation, we have a simple struct defined to hold font and cell size properties:
[[See Video to Reveal this Text or Code Snippet]]
To enable reading and writing this struct with QVariant, we've implemented the necessary operators for serialization (the operators << and >> for QDataStream) and equality checking.
The Problematic Code
The writer code seems correct and is set up as follows:
[[See Video to Reveal this Text or Code Snippet]]
However, when attempting to read the value back:
[[See Video to Reveal this Text or Code Snippet]]
it consistently returns the default values of {0, 0}. This indicates a failure in the type conversion from QVariant back to your custom struct.
Dissecting the Failure
Initially, you might think that your reading logic is sound. Let’s go through the process step-by-step:
Setting the Value: Your write operation seems functional since the output in your INI file corresponds with the expected UI changes.
Getting the Value: Despite your structured approach, upon retrieval, font and cell values turn out as {0, 0}, hinting at a failure in correctly retrieving the stored data.
Breakpoints Insight: Your debugging revealed that, interestingly, the operator>> is triggered after the read attempts, displaying the correct values but still assigning them incorrectly to the struct.
The Solution: Registering Your Custom Type
To successfully read and write custom types using QVariant, you need to register your type with Qt's metatype system. This step informs Qt about your custom type's existence and how to properly manage it, particularly when it comes to storing/retrieving in QSettings.
Simple Fix
You simply need to add the following line in the constructor of your CharTableView class:
[[See Video to Reveal this Text or Code Snippet]]
This enables the registration of your CellSize struct, allowing QVariant to correctly handle it.
Conclusion: Wrapping It All Up
Understanding how to manage custom data types in Qt can be particularly tricky, especially when dealing with QVariant. The key is ensuring that your types are correctly registered with the Qt meta-object system so that all operations involving them — from QSettings to data streams — work seamlessly.
Now that you know how to resolve the issue of reading values from QSettings, go ahead and apply this knowledge to your projects! Happy coding!
Информация по комментариям в разработке