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 8596139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:36:20+00:00 2026-06-12T00:36:20+00:00

Consider the following Qt code: class Foo : public QObject { Q_OBJECT Q_ENUMS(E) Q_PROPERTY(E

  • 0

Consider the following Qt code:

class Foo : public QObject {
  Q_OBJECT
  Q_ENUMS(E)
  Q_PROPERTY(E x READ x WRITE set_x)

  public:
    enum E {
      a = 0,
      b = 1,
      c = 2
    };

    E x() const { return x_; }
    void set_x(E value) { x_ = value; }

private:
    E x_;
};

int main (int argc, char **argv) {
  QCoreApplication app(argc, argv);

  Foo f;

  f.setProperty("x", Foo::c);
  std::cout << f.property("x").toInt() << std::endl;  // 2

  f.setProperty("x", QVariant((int)1));
  std::cout << f.property("x").toInt() << std::endl; // 1

  f.setProperty("x", QVariant((long long)0));
  std::cout << f.property("x").toInt() << std::endl; // should be 0. is 1. 
}

Why does it work that way?

  • 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-12T00:36:21+00:00Added an answer on June 12, 2026 at 12:36 am

    If you test the return value of setProperty, you will see that the set is failing:

    ok = f.setProperty("x", QVariant((long long)0));
    std::cout << ok << std::endl;  // 0, i.e. false
    

    The relevant part of the Qt code is in qmetaobject.cpp, annotated below:

    if (isEnumType()) {
        if (v.type() == QVariant::String) {
            // ... we won't get here.
        } else if (v.type() != QVariant::Int && v.type() != QVariant::UInt) {
            // We got here because we didn't provide an int or uint.
    
            // This will be 0...
            int enumMetaTypeId = QMetaType::type(qualifiedName(menum));
    
            // ... which means this will return false; the property will not be set.
            if ((enumMetaTypeId == 0) ||
                (v.userType() != enumMetaTypeId) ||
                !v.constData())
                return false;
    
            // ... we never get here
        }
    }
    
    // ... we never get here
    

    So the behavior seems to be by design: enum properties can only be set using QVariant objects with a type of int or uint.

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

Sidebar

Related Questions

Consider the following code: class A { public: A& operator=( const A& ); const
Consider the following short code snippet. namespace B { public class Foo { public
Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following code: public class Foo { private static final Object LOCK =
Consider the following code: class CString { private: char* buff; size_t len; public: CString(const
Consider the following code class X{ public: virtual void foo(X x){ } virtual void
Consider the following code: #include <iostream> class Bar { public: void foo(bool b =
Consider the following code (C# 4.0): public class Foo : LambdaExpression { } This
Consider the following code: class A { public: void foo() { auto functor =
Consider the following code: public class Bar { Foo foo; void Go() { foo

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.