Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8018093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:02:33+00:00 2026-06-04T21:02:33+00:00

I write a library which uses Qt’s meta type system. The user of my

  • 0

I write a library which uses Qt’s meta type system. The user of my library can provide values of custom types which get wrapped in a QVariant. Later in some code internally in the library, I need to serialise this QVariant. I implemented serializers for a couple of types and I really don’t need to support every type known to the Qt meta type system. I serialise the values in order to send them over the network using JSON.

But what I’d like to achieve is to support custom enum types, which the user has to register to the meta type system using qtRegisterMetaType<MyEnum>("MyEnum"); anyway. I want to serialise such an enum via converting it to an integer type.

I tried QVariant::value<int>() on such an enum value, but it always returns 0. QVariant::canConvert<int>() returns false.

Is it possible (maybe using some dirty hacking) to get to the integer value of the wrapped enum? How does Qt handle enum values in its meta type system? Maybe I can just access the raw void* pointer when I’m sure that this is an enum value I have here.

This leads us to the next question: How to check if a Qt meta type is a user enum type at all?

Please note that since I’m writing internal code of the library, I can’t use templates. The only “thing” I have is the QVariant, no compile-time type information.

// code in the project using my library:
MyEnum foo;
libraryFunction(foo);
// internal library code (note that compile-time type info isn't available):
QVariant foo = getValueAsVariant(...);
if (/* how to check if foo is an enum type? */)
    int enumValue = foo.value<int>(); // not working for enums!

I know that the C++ compiler can choose to use any integer type (not only 32-bit ones) internally to store enum values. Can this be a problem for me? (If I reinterpret the void* as int* for example. But remember that the void* data is allocated by QVariant itself, so it may allocate more than, for example, 2 bytes for a enum with sizeof == 2.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:02:36+00:00Added an answer on June 4, 2026 at 9:02 pm

    A dirty hack would be to serialize the QVariant to a QDataStream, then deserialize the last 4 bytes to an int. This doesn’t answer the question of how to check for an enum type. I’ll dig deeper into the moc output and update this.

    Leemes has helpfully found that QMetaObject::enumerator() will give you all meta enums defined in a class. This is enough for checking if a given type name is an enum or not, as one can convert the enum names to the type IDs using QMetaType::type() (but prepending the class name and “::”). In order to convert from QVariant holding an enum to an int, one simply reinterprets the QVariant::constData() as an int pointer and reads it. It works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wanting to write a static library which uses RestKit , for an
I have a library, which uses Common.Logging. I need to get my code called
TickZoom is a very high performance app which uses it's own parallelization library and
HellO, I want to write a binary file format viewer for windows which can
I want to write application which uses Postgresql as DBMS. To write client application
I wrote a program which uses Cryptopp library. I have created the static library
The Linux kernel uses lock; addl $0,0(%%esp) as write barrier, while the RE2 library
I'm creating a Django library which uses optimistic concurrency control to prevent concurrent writes
I am trying to write my own MySQL storage engine which uses a key-value
I've run into an issue which I'm not sure if I can get round

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.