Skip to content

Add null QVariant constructor #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

direc85
Copy link
Contributor

@direc85 direc85 commented Aug 5, 2024

QVariant::default() yields an undefined value in QML, but in many places a null value is more suited. Add QVariant::null() function to achieve that. The test added help distinguishing this.

I would have used QVariant(nullptr) instead, but that doesn't seem to work well with the type system in Qt5. The QJsonValue().toVariant() is the next best option - and makes sense, since a JSON can also have an explicit null value.

@direc85 direc85 force-pushed the null-qvariant branch 2 times, most recently from e26c6b0 to 9fe1dc3 Compare August 5, 2024 10:03
@ogoffart
Copy link
Member

Sorry for not having answer to this PR before.

I don't think it makes sense to construct a QVariant with a JSValue from this API. If you want a JSValue, IMHO you should construct a JSValue and convert that to a QVariant.

@direc85
Copy link
Contributor Author

direc85 commented Jan 18, 2025

I have no idea how the test was a merge conflict, but it's now rebased to master.

I agree, it doesn't really make sense to construct a null QVariant from QJsonValue, but that was the most elegant way I was able to find for Qt 5, since QVariant::fromValue(nullptr) doesn't work until Qt 6. I mitigated this by making it pick the choice compile time, that's something at least.

But now that I think it twice, and browsed the codebase more, it makes more sense to use QJsonValue.toVariant() as it's already available. I think I'll explore that for a bit to see that's usable already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants