PDA

View Full Version : enum property



illuzioner
14th February 2006, 22:24
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:


// a header file
enum EditType {Currency, Percent, LabeledNumber, Text};
//plugin widget file
Q_ENUMS(EditType)
Q_PROPERTY(EditType LineType READ GetLineType WRITE SetLineType DESIGNABLE true);
public:
EditType GetLineType() const {return LineType;}
protected:
EditType LineType;
public slots:
void SetLineType (const EditType);

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

jacek
14th February 2006, 22:43
Maybe you should try something like this:
class SomeClass : public ...
{
Q_OBJECT
Q_ENUMS(EditType)
Q_PROPERTY(EditType LineType READ GetLineType WRITE SetLineType DESIGNABLE true);
public:
enum EditType {Currency, Percent, LabeledNumber, Text};
EditType GetLineType() const {return LineType;}

protected:
EditType LineType;

public slots:
void SetLineType (const EditType);
// ...
};

illuzioner
14th February 2006, 22:58
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?? :confused:

newby
10th August 2006, 19:09
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?

Valheru
22nd August 2006, 11:20
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?? :confused:

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};

jpn
22nd August 2006, 11:39
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.



class SomeClass
{
enum Enum { Val1, Val2 }; // this is a private enum, cannot access "outside"

public:
SomeClass();
...
};

class AnotherClass
{
public:
enum Enum { Val1, Val2 }; // this is a public enum, accessible from "outside"

AnotherClass();
...
};



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..

Valheru
22nd August 2006, 14:34
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.

jacek
22nd August 2006, 15:22
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.

SomeClass::SomeEnum SomeClass::foo() { return SomeEnumValue; }
^^^^^^^^^^^
If you are talking about this, then it certainly isn't inside.

Valheru
22nd August 2006, 19:18
No, I'm talking about this:


//Header file connection.h

#ifndef CONNECTION_H
#define CONNECTION_H

#include <QTcpSocket>

class Connection() : public QTcpSocket
{
Q_OBJECT
Q_ENUMS( Listing )
Q_PROPERTY( Listing listing READ listing WRITE setListing )
public:
Connection( const QString&, const quint16& );
~Connection();
enum Listing{ NOT_LISTING = 0, LISTING_ARTICLES = 1, LISTING_GROUPS = 2 };
Listing listing() const;
void setListing( Listing l);
private:
Listing lst;
};

#endif //CONNECTION_H

//connection.cpp

#include "connection.h"

...

Connection::Listing Connection::listing() const
{
return lst;
}

void Connection::setListing( Listing l )
{
lst = l;
}

...

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.

jacek
22nd August 2006, 20:45
No, I'm talking about this
Well... I would say we talk about the same thing.

Anyway here's my explanation:


Connection::Listing Connection::listing() const
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.


void Connection::setListing( Listing l )
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.

Valheru
22nd August 2006, 22:47
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.