Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Scoped enums, Meta Object and Stylesheets

Scoped enums, Meta Object and Stylesheets

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NicolasS
    wrote on last edited by NicolasS
    #1

    Dear all,

    How can I register a scoped enum to the meta-object system ?
    I'd like be able to change the value of a scoped enum property via stylesheet but i'm not able to make my code work:

    class MyClass : public QWidget
    {
    	Q_OBJECT;
    	Q_ENUMS(Priority);
    
    	Q_PROPERTY(Priority ThePriority MEMBER priority);
    
    public:
    	enum struct Priority { High, Low, VeryHigh, VeryLow };
    
    
    	MyClass(QObject *parent = 0) {}
    
    	~MyClass() {}
    
    	void setPriority(Priority ipriority)
    	{
    		priority = ipriority;
    	}
    
    	Priority getPriority() const
    	{
    		return priority;
    	}
    
    private:
    	Priority priority { Priority::High };
    };
    
    int main(int argc, char *argv[])
    {
    	QApplication a(argc, argv);
    	a.setStyleSheet(QStringLiteral("MyClass { qproperty-ThePriority: VeryLow;}"));
    	
    	MyClass myClass;
    	myClass.ensurePolished();
    	
    	Q_ASSERT(myClass.getPriority() == MyClass::Priority::VeryLow); // ASSERT! getPriority() == MyClass::Priority::High
    
    	return a.exec();
    }
    

    Any idea ?
    Regards
    Edit: Fix errors in source code sample

    kshegunovK 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi,

      AFAIK, you can't set a property through style sheet. You can create a style sheet that reacts on a property change though.

      [edit: memory was wrong SGaist]

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        NicolasS
        wrote on last edited by
        #3

        Yes you can: Setting QObject Properties

        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @NicolasS Thanks for the pointer !

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • N Offline
            N Offline
            NicolasS
            wrote on last edited by NicolasS
            #5

            This code works well if I use classic enum: enum Priority { High, Low, VeryHigh, VeryLow };

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Which version of Qt are you using ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N NicolasS

                Dear all,

                How can I register a scoped enum to the meta-object system ?
                I'd like be able to change the value of a scoped enum property via stylesheet but i'm not able to make my code work:

                class MyClass : public QWidget
                {
                	Q_OBJECT;
                	Q_ENUMS(Priority);
                
                	Q_PROPERTY(Priority ThePriority MEMBER priority);
                
                public:
                	enum struct Priority { High, Low, VeryHigh, VeryLow };
                
                
                	MyClass(QObject *parent = 0) {}
                
                	~MyClass() {}
                
                	void setPriority(Priority ipriority)
                	{
                		priority = ipriority;
                	}
                
                	Priority getPriority() const
                	{
                		return priority;
                	}
                
                private:
                	Priority priority { Priority::High };
                };
                
                int main(int argc, char *argv[])
                {
                	QApplication a(argc, argv);
                	a.setStyleSheet(QStringLiteral("MyClass { qproperty-ThePriority: VeryLow;}"));
                	
                	MyClass myClass;
                	myClass.ensurePolished();
                	
                	Q_ASSERT(myClass.getPriority() == MyClass::Priority::VeryLow); // ASSERT! getPriority() == MyClass::Priority::High
                
                	return a.exec();
                }
                

                Any idea ?
                Regards
                Edit: Fix errors in source code sample

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                From your link you would need to use: qproperty-priority instead.
                If this doesn't work (and it's just a wild guess) but could you register that enum as an alias in the metatype system, e.g. qRegisterMetatype<Priority>("Priority") in your class' constructor.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  NicolasS
                  wrote on last edited by
                  #8

                  @SGaist
                  I'm using Qt 5.9.4 x64

                  @kshegunov
                  You are right, there was typo in my sample. I've edited the code from the main post.
                  I had no success with qRegisterMetatype, the code doesn't compile.
                  As you suggested, I've tried this:

                  MyClass(QObject *parent = 0)
                  {
                     qRegisterMetatype<Priority>("Priority")
                  }
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Please use enum class.

                    moc currently doesn't support enum struct, there's a patch in the work though for that.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    2
                    • N Offline
                      N Offline
                      NicolasS
                      wrote on last edited by
                      #10

                      It works!
                      Thanks.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved