PDA

View Full Version : Qt Designer How to use custom enum in designer that be decladred in an other class



clebail
10th October 2011, 12:52
Hi,

Sorry for my English, I'm French...

I've got a class, like this


class QDESIGNER_WIDGET_EXPORT CMyLabel : public QLabel
{
Q_OBJECT
Q_PROPERTY(EDataType dataType READ getDataType WRITE setDataType)
Q_ENUMS(EDataType)
public:
enum EDataType {edtString=0, edtInt, edtDouble, edtDate};

CMyLabel(QWidget *parent = 0);
EDataType getDataType(void) { return _dataType; }
void setDataType(EDataType dataType) { _dataType=dataType; }
private:
EDataType _dataType;
};

Which works very well in designer, the EDataType enum is visible in properties editor for property dataType.

Because EDataType is used in other class, I want to declare EDataType outside the class, so, as stated in the doc

If you want to register an enum that is declared in another class, the enum must be fully qualified with the name of the class defining it. In addition, the class defining the enum has to inherit QObject as well as declare the enum using Q_ENUMS().
I do this:



class CFoo : public QObject
{
Q_OBJECT
public:
enum EDataType {edtString=0, edtInt, edtDouble, edtDate};
};


class QDESIGNER_WIDGET_EXPORT CMyLabel : public QLabel
{
Q_OBJECT
Q_PROPERTY(CFoo::EDataType dataType READ getDataType WRITE setDataType)
Q_ENUMS(CFoo::EDataType)
public:
CMyLabel(QWidget *parent = 0);
CFoo::EDataType getDataType(void) { return _dataType; }
void setDataType(CFoo::EDataType dataType) { _dataType=dataType; }
private:
CFoo::EDataType _dataType;
};

It compile very well, but when I launch designer, I've got this error:

And the EDataType enum is not visible in properties editor.

QMetaProperty::read: Unable to handle unregistered datatype 'CFoo::EDataType' for property 'CMyLabel::dataType'


How can i do this ?

Thanks

Corentin

clebail
10th October 2011, 16:02
Hi,

I solved it alone, simply like this



class CFoo : public QObject
{
Q_OBJECT
Q_ENUMS(EDataType)
public:
enum EDataType {edtString=0, edtInt, edtDouble, edtDate};
};


class QDESIGNER_WIDGET_EXPORT CMyLabel : public QLabel
{
Q_OBJECT
Q_PROPERTY(CFoo::EDataType dataType READ getDataType WRITE setDataType)
Q_ENUMS(CFoo::EDataType)
public:
CMyLabel(QWidget *parent = 0);
CFoo::EDataType getDataType(void) { return _dataType; }
void setDataType(CFoo::EDataType dataType) { _dataType=dataType; }
private:
CFoo::EDataType _dataType;
};

wysota
10th October 2011, 16:50
I think you don't need line #14 in your code.