PDA

View Full Version : Unable to define a new type with QVariant



max2g
23rd February 2008, 13:05
Can someone tell me what's wrong with the following code:


GKey gg1(1, "Hi");

int id = qRegisterMetaType<GKey>("GKey");

QVariant testV(id, &gg1);

GKey gg2;

if (testV.canConvert<GKey>())
gg2 = testV.value<GKey>();

The last line gives the following error :


"error: no matching function for call to `GKey::GKey(GKey)"

The GKey.h file contains :


#ifndef KEY_H
#define KEY_H

#include "SmartPtr.h"
#include <QList>
#include <QMetaType>

class GKey : public QObject
{
public:
GKey();
GKey(long type, QString uuid);
virtual ~GKey();

const GKey& operator=(const GKey& key);
virtual bool operator==(const GKey& key) const;
virtual bool operator!=(const GKey& key) const;

inline long getType() { return type_; };
void setType(long type) {type_ = type;};

inline QString getUuid() { return uuid_; };
void setUuid(QString uuid) {uuid_ = uuid;};

static long getMetaNo(){ return 6000;};

explicit GKey(const GKey& key);
explicit GKey(const GKey* key);

void copy(const GKey& key);
void free();

void initializeGKey(long constructorType);
void finishGKey(long constructorType);

protected:
long type_;
QString uuid_;

long constructorType_;
};

Q_DECLARE_METATYPE(GKey)

typedef SmartPtr<GKey> SmartPtrGKey;
typedef QList< SmartPtrGKey > SmartPtrGKeyList;


#endif


Thank you !

jpn
23rd February 2008, 14:46
Why explicit copy constructor?

max2g
23rd February 2008, 17:49
The EXPLICIT was required because I don't want to use these method when implicit conversion is required but this seems to be the problem !

If I remove the EXPLICIT keyword, the compilation is Ok !

I saw that my original post was edited. Is there a special procedure to follow when a post must be edited ????

Thank you !

Max2G

jpn
23rd February 2008, 17:55
I saw that my original post was edited. Is there a special procedure to follow when a post must be edited ????
I added [code] and [quote] tags to make it a bit more readable. Wrapping code snippets with [code] tags (the "#" button in editor) produces line numbers and syntax highlighting plus allows indentation, thus makes code snippets more readable. For more details: http://www.qtcentre.org/forum/misc.php?do=bbcode