Seite 1 von 1

QVariant zu custom enum casten?

Verfasst: 27. Mai 2008 18:48
von -=Freaky=-
hallo,
ich habe hier im forum schon gelesen, dass man ein QVariant (richtige daten vom typ d. eigenen enums wurden uebergeben) per toInt() zu einer solchen (passenden) enum-variable konvertieren kann.

laut doku sollte aber dieses kleine programm funktionieren:
main.cpp:

Code: Alles auswählen

#include "myClass.h"
#include <QApplication>

int main( int argc, char **argv )
{
	QApplication app( argc, argv );
	
	myClass cl1;
	cl1.set( myClass::OPT_ONE, 1337 );
	cl1.set( myClass::OPT_TWO, 42.1337 );
	cl1.set( myClass::OPT_ENUM, QVariant::fromValue( myClass::ENUM_VALUE_TWO ) );
	
	return 0;
}
myClass.h:

Code: Alles auswählen

#include <QObject>
#include <QVariant>
#include <QMap>
#include <QDebug>

class myClass : public QObject
{
	Q_OBJECT
	Q_ENUMS( myClass::myOption )
	Q_ENUMS( myClass::myEnum )
	public:
		enum myOption{ OPT_ONE, OPT_TWO, OPT_ENUM };
		enum myEnum{ ENUM_VALUE_ONE = 0, ENUM_VALUE_TWO = 1 };
		myClass( QObject *parent = 0 );
		
	public slots:
		QVariant get( const myOption p_opt ) const;
		void set( const myOption p_opt, const QVariant &p_val );
		
	private:
		QMap< myOption, QVariant > map;
};
Q_DECLARE_METATYPE( myClass::myOption )
Q_DECLARE_METATYPE( myClass::myEnum )
myClass.cpp:

Code: Alles auswählen

#include "myClass.h"

myClass::myClass(QObject *parent)
	:QObject( parent )
{
	map.insert( OPT_ONE, QVariant( 42 ) );
	map.insert( OPT_TWO, QVariant( 13.37 ) );
	map.insert( OPT_ENUM, QVariant::fromValue( myClass::ENUM_VALUE_ONE ) );
}

QVariant myClass::get( const myOption p_opt ) const
{
	return map[ p_opt ];
}

void myClass::set( const myOption p_opt, const QVariant &p_val )
{
	switch( p_opt )
	{
		case OPT_ONE:
			if( p_val.canConvert< int >() )
				map[ OPT_ONE ] = p_val;
			else
				qDebug() << "can't be converted to int!";
			break;
		case OPT_TWO:
			if( p_val.canConvert< double >() )
				map[ OPT_TWO ] = p_val;
			else
				qDebug() << "can't be converted to double!";
		case OPT_ENUM:
			if( p_val.canConvert< myClass::myEnum >() )
				map[ OPT_ENUM ] = p_val;
			else
				qDebug() << "can't be converted to myClass::myEnum!";
	}
}
die ausgabe ist aber in meinem fall (linux, gcc-4.1.1-r3, Qt-4.3.4) immer

Code: Alles auswählen

can't be converted to myClass::myEnum!
nach qt doku (bool QVariant::canConvert () const bzw. QVariant QVariant::fromValue ( const T & value ) [static], da stehen beispiele) sollte es m.e. funktionieren, oder habe ich etwas missverstanden?

mfg,
julian

Verfasst: 27. Mai 2008 18:56
von Sephral
Meinst du sowas?

Code: Alles auswählen

enum eState 
{
	eStateOk	= 0,
	eStateError	= 1,
};

Code: Alles auswählen

QVariant v(1);
eState irgendwas = (eState)v.toInt();
...
if(irgendwas == eStateOk)
{
    ...
}

Ciao,
Sephral

Verfasst: 27. Mai 2008 19:47
von -=Freaky=-
eben nicht, das ist halt der umweg ueber int (der c-cast zu eState waere doch nichtmal noetig afaik?).
ich moechte aber eben wissen, ob das auch so funktioniert, ohne int, mit direkter angabe d. bezeichners vom enum.
wie gesagt, laut doku sollte es, sofern ich das richtig verstanden habe, funktionieren. oder gibt es spezielle ausnahmen fuer enums? Q_REGISTER_METATYPE habe ich ja genutzt ...

mfg,
julian