Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, March 2008
extending QVariant to custom types


Message 1 in thread

I've got a simple class that pretty much holds a number, and string to
represent units (feet, meters and such).  I'd like to use this class
with a widget I created which is compiled as a plug-in.  I designed my
widget to work with QVariants hoping it would be flexible enough to let
me do what I wanted.  Well QVariants can't convert custom types to
anything at all, so in order to call my custom type's toString function
I would need to use QVariant's templated value function.  But that
requires me to link my widget against my custom class, which is a deal
breaker because I expect to need a lot more custom classes down the
road.

 

So, to put it simply, I need a way to have QVariant turn my custom class
in to a string?  Would it be a good idea to modify the QVariant class?
I did some digging and found a method (qRegisterMetaTypeStreamOperators)
to register overloaded stream operators for QVariants, but these only
help if you wanted to save them in a binary format, is there a similar
method for character based QVariant stream operators?

 

Thanks for reading,

Joaquin Luna


Message 2 in thread

On Friday 14 March 2008 19:09:38 Joaquin Luna wrote:
> I did some digging and found a method (qRegisterMetaTypeStreamOperators)
> to register overloaded stream operators for QVariants, but these only
> help if you wanted to save them in a binary format, is there a similar
> method for character based QVariant stream operators?
what else do you need?  why do you care how Qvariant stores them internaly?
-- 
 [ signature omitted ] 

Message 3 in thread

I guess I could have been clearer when stating my problem.  Ideally I would like a code snippet like the one below to work for user defined classes like the one I described earlier.

MyCustomClass x;
QVariant v = QVariant::fromValue(x);

QString s = v.toString();


I have overloaded the stream operators and registered them with the metatype system, but that servers a much different purpose than the one above.

-----Original Message-----
From: Arvid Ephraim Picciani [mailto:aep@xxxxxxxxxxxxxxx] 
Sent: Friday, March 14, 2008 2:59 PM
To: qt-interest@xxxxxxxxxxxxx
Subject: Re: extending QVariant to custom types

On Friday 14 March 2008 19:09:38 Joaquin Luna wrote:
> I did some digging and found a method (qRegisterMetaTypeStreamOperators)
> to register overloaded stream operators for QVariants, but these only
> help if you wanted to save them in a binary format, is there a similar
> method for character based QVariant stream operators?
what else do you need?  why do you care how Qvariant stores them internaly?
-- 
 [ signature omitted ] 

Message 4 in thread

On Friday 14 March 2008 20:17:23 you wrote:
> I guess I could have been clearer when stating my problem.  Ideally I would
> like a code snippet like the one below to work for user defined classes
> like the one I described earlier.
>
> MyCustomClass x;
> QVariant v = QVariant::fromValue(x);
>
> QString s = v.toString();
you need the string human readable?
if not just use QMetaType::save
if you do require human readable strings, well,... i'm still kinda hoping for 
a builtin solution too, so we can serialise xml more easy. for now you need 
to define your own operators (for QTextStream)

-- 
 [ signature omitted ] 

Message 5 in thread

Yes, I am trying to display a human readable string for a QVariant holding a custom type.  I have defined operators for my custom class as below.

friend QTextStream & operator<<(QTextStream &out, const SC3P_Measurement & myObj);
friend QTextStream & operator>>(QTextStream &in, SC3P_Measurement & myObj);


Is this supposed to allow me to stream out QVariants containing my custom types?  I have tried it and it does not work for me, maybe you could provide more details on how this is supposed to work.

-----Original Message-----
From: Arvid Ephraim Picciani [mailto:aep@xxxxxxxxxxxxxxx] 
Sent: Friday, March 14, 2008 3:24 PM
To: qt-interest@xxxxxxxxxxxxx
Subject: Re: extending QVariant to custom types

On Friday 14 March 2008 20:17:23 you wrote:
> I guess I could have been clearer when stating my problem.  Ideally I would
> like a code snippet like the one below to work for user defined classes
> like the one I described earlier.
>
> MyCustomClass x;
> QVariant v = QVariant::fromValue(x);
>
> QString s = v.toString();
you need the string human readable?
if not just use QMetaType::save
if you do require human readable strings, well,... i'm still kinda hoping for 
a builtin solution too, so we can serialise xml more easy. for now you need 
to define your own operators (for QTextStream)

-- 
 [ signature omitted ] 

Message 6 in thread

On Friday 14 March 2008 21:05:06 Joaquin Luna wrote:
> Yes, I am trying to display a human readable string for a QVariant holding
> a custom type.  I have defined operators for my custom class as below.
>
> friend QTextStream & operator<<(QTextStream &out, const SC3P_Measurement &
> myObj); friend QTextStream & operator>>(QTextStream &in, SC3P_Measurement &
> myObj);
>
>
> Is this supposed to allow me to stream out QVariants containing my custom
> types?  I have tried it and it does not work for me, maybe you could
> provide more details on how this is supposed to work.
huh? err... maybe _you_ could provide more details. You don't exactly get 
humanreadable strings out of QVariant. unless you qvariant_cast back to your 
custom type and use the text operators then. you might as well just add a 
toString() function to your class.

-- 
 [ signature omitted ]