Results 1 to 11 of 11

Thread: enum property

  1. #1
    Join Date
    Feb 2006
    Posts
    29
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Windows

    Default enum property

    hi all,

    i have a qproperty enum

    enum EditType {Currency, Percent, LabeledNumber, Text};

    that i need for a custom widget plugin. in the properties examples in the docs it defines the enum within the body of the class. my enum is used by a number of classes so it needs to be defined in a header file outside the class and included so all classes can see it.

    the docs state that the enum must be "declared in the class itself". i do this as it states with the Q_ENUMS(EditType) macro (unless it's talking about the above enum statement). then i implement the other qproperty statements so i have:

    Qt Code:
    1. // a header file
    2. enum EditType {Currency, Percent, LabeledNumber, Text};
    3. //plugin widget file
    4. Q_ENUMS(EditType)
    5. Q_PROPERTY(EditType LineType READ GetLineType WRITE SetLineType DESIGNABLE true);
    6. public:
    7. EditType GetLineType() const {return LineType;}
    8. protected:
    9. EditType LineType;
    10. public slots:
    11. void SetLineType (const EditType);
    To copy to clipboard, switch view to plain text mode 

    everything compiles and links fine, but the problem is that while LineType shows up in the properties in designer, it's blank and and there's no way to change it.

    anyone know what's wrong and how to fix it?

    thanks!
    lou

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: enum property

    Maybe you should try something like this:
    Qt Code:
    1. class SomeClass : public ...
    2. {
    3. Q_OBJECT
    4. Q_ENUMS(EditType)
    5. Q_PROPERTY(EditType LineType READ GetLineType WRITE SetLineType DESIGNABLE true);
    6. public:
    7. enum EditType {Currency, Percent, LabeledNumber, Text};
    8. EditType GetLineType() const {return LineType;}
    9.  
    10. protected:
    11. EditType LineType;
    12.  
    13. public slots:
    14. void SetLineType (const EditType);
    15. // ...
    16. };
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Posts
    29
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: enum property

    this is what i had initially, but the problem with this is that it doesn't allow me to use the EditType enum in other classes. plus, now the EditType enum becomes the class type requiring the :: operator whenever i need to refer to the enum.

    other ideas??
    Last edited by illuzioner; 14th February 2006 at 22:00. Reason: more info

  4. #4
    Join Date
    Jun 2006
    Location
    USA
    Posts
    6
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: enum property

    I am running into the same problem regarding the enum properties.
    I had sent email to TrollTech and they replied back that there is no other way to
    use enum properties.

    Have you found any work around for this problem?

  5. #5
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: enum property

    Quote Originally Posted by illuzioner
    this is what i had initially, but the problem with this is that it doesn't allow me to use the EditType enum in other classes. plus, now the EditType enum becomes the class type requiring the :: operator whenever i need to refer to the enum.

    other ideas??
    Err, I've JUST done that in my project and I can access the enum thypes in other classes, as long as I include the header file.
    I'll agree that having to use the class :: prefix in the .cpp file of the class implementing the enum is retarded, but that's just a minor asthetic thing.

    What I did find FUBAR is that Qt assignes really retarded, non-logical values (ie. not beginning at 0 and increasing from there) in the outputted moc file, so I ended up forcing it to do so by passing the values in the initialisation of the enum. ie. enum EnumInstance { foo = 0, bar = 1, fubar = 2};

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: enum property

    Quote Originally Posted by Valheru
    Err, I've JUST done that in my project and I can access the enum thypes in other classes, as long as I include the header file.
    Yes, you can access it as long as the enum is declared as public.

    Qt Code:
    1. class SomeClass
    2. {
    3. enum Enum { Val1, Val2 }; // this is a private enum, cannot access "outside"
    4.  
    5. public:
    6. SomeClass();
    7. ...
    8. };
    9.  
    10. class AnotherClass
    11. {
    12. public:
    13. enum Enum { Val1, Val2 }; // this is a public enum, accessible from "outside"
    14.  
    15. AnotherClass();
    16. ...
    17. };
    To copy to clipboard, switch view to plain text mode 

    I'll agree that having to use the class :: prefix in the .cpp file of the class implementing the enum is retarded, but that's just a minor asthetic thing.
    This is a C++, not Qt "issue". An enumeration defined inside a class belongs to the namespace of the class. You'll need to define the enum outside the class to be able to use it without a namespace prefix..
    J-P Nurmi

  7. #7
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: enum property

    This is a C++, not Qt "issue". An enumeration defined inside a class belongs to the namespace of the class. You'll need to define the enum outside the class to be able to use it without a namespace prefix..
    Yes, but then you would think that you WOULDN'T have to use the class prefix INSIDE the class then. But if I want to return a result from a member function of the class in which the enum is decalred as a type of the enum, I find it illogical that I should have to sue the class prefix.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: enum property

    Quote Originally Posted by Valheru
    Yes, but then you would think that you WOULDN'T have to use the class prefix INSIDE the class then. But if I want to return a result from a member function of the class in which the enum is decalred as a type of the enum, I find it illogical that I should have to sue the class prefix.
    Qt Code:
    1. SomeClass::SomeEnum SomeClass::foo() { return SomeEnumValue; }
    2. ^^^^^^^^^^^
    To copy to clipboard, switch view to plain text mode 
    If you are talking about this, then it certainly isn't inside.

  9. #9
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: enum property

    No, I'm talking about this:

    Qt Code:
    1. //Header file connection.h
    2.  
    3. #ifndef CONNECTION_H
    4. #define CONNECTION_H
    5.  
    6. #include <QTcpSocket>
    7.  
    8. class Connection() : public QTcpSocket
    9. {
    10. Q_OBJECT
    11. Q_ENUMS( Listing )
    12. Q_PROPERTY( Listing listing READ listing WRITE setListing )
    13. public:
    14. Connection( const QString&, const quint16& );
    15. ~Connection();
    16. enum Listing{ NOT_LISTING = 0, LISTING_ARTICLES = 1, LISTING_GROUPS = 2 };
    17. Listing listing() const;
    18. void setListing( Listing l);
    19. private:
    20. Listing lst;
    21. };
    22.  
    23. #endif //CONNECTION_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //connection.cpp
    2.  
    3. #include "connection.h"
    4.  
    5. ...
    6.  
    7. Connection::Listing Connection::listing() const
    8. {
    9. return lst;
    10. }
    11.  
    12. void Connection::setListing( Listing l )
    13. {
    14. lst = l;
    15. }
    16.  
    17. ...
    To copy to clipboard, switch view to plain text mode 
    I find it illogical that the return value of the function listing() has to be written as Connection::Listing when you are accessing it from the class implementation. Maybe it's just a gcc quirk, I don't know. But it doesn't bitch about accessing it as "Listing" anwhere else (as seen in the header file). Just when returning it from the function.
    Maybe it's just some ANSI c++ rule I've never encountered since I don't normally use enums to do things. But I thought it was wierd when I ran into it a few days ago.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: enum property

    Quote Originally Posted by Valheru
    No, I'm talking about this
    Well... I would say we talk about the same thing.

    Anyway here's my explanation:

    Qt Code:
    1. Connection::Listing Connection::listing() const
    To copy to clipboard, switch view to plain text mode 
    Here the enum is used outside the class definition and when compiler reads the source file it doesn't know that it's a type of a return value of some method. All it sees is "Connection::Listing" and it expects it to be a valid type. "Listing" isn't declared in global scope, so you must prefix it with class name.

    Qt Code:
    1. void Connection::setListing( Listing l )
    To copy to clipboard, switch view to plain text mode 
    Here when compiler reaches "Listing", it should already know that it reads a signature of the setListing() method from Connection class, so it should know also that Listing is a valid type.

    You can always prefix Listing with class name if you want.

    Of course it's only my point of view and if you want an authoritative answer you should consult C++ specification, but still it has nothing to do with Qt and there's nothing we can do about it.

  11. #11
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: enum property

    It is very possible that it's a C++ issue, as I said I don't use enums normally. I don't really mind either way, it just struck me as passing strange that it accpted it in the declaration of the function in the header file, but choked on it in the implementation in the .cpp file.
    It's not a problem either - as I said I call the enum(prefixed by the class name) from other class' member and all works fine, so in response to the original question in the thread, yes it is possbile.

Similar Threads

  1. Adding property editor file browser popup...
    By thawkins in forum Qt Tools
    Replies: 1
    Last Post: 25th April 2007, 23:03
  2. FrameShape property for Q3GroupBox
    By user_mail07 in forum Qt Tools
    Replies: 3
    Last Post: 16th February 2007, 14:41
  3. QPushbutton Flat property and icon appearance.
    By darpan in forum Qt Programming
    Replies: 1
    Last Post: 4th November 2006, 08:30
  4. QPixmap designer property not showing up
    By high_flyer in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2006, 19:56
  5. enum scope
    By illuzioner in forum General Programming
    Replies: 1
    Last Post: 15th February 2006, 05:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.