Trolltech Home | QSA-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

QSA-interest Archive, May 2006
QVariant, custom user types and interacting with C++


Message 1 in thread

What can I do to get custom user type value into C++ slot from Qsa?

I want to use my custom DataTypes with Qsa and C++, like whis:

/////////////////////////////
class GlobalRegisterInterface : public QObject
{
    Q_OBJECT
public:
    GlobalRegisterInterface()
    {
          setObjectName("GRI");
    }

public slots:
    QVariant* value(int valueId);
    void setValue(int valueId, QVariant* value);
};

....

theQSProject->addObject(new GlobalRegisterInterface(theGlobalRegister));
theQSProject->interpreter()->addObjectFactory(new DataTypeFactory());

/////////////////////////////
value integer: IntegerDataType;

value = new IntegerDataType(300);
GRI.setValue(200, value);
value = GRI.value(100);
/////////////////////////////

But then I run program in GlobalRegisterInterface::setValue(...) I get 
default constructered IntegerDataType, but not IntegerDataType(300).



Some useful sources:

/////////////////////////////////////////////////////////////////////////
class IntegerDataType : public QObject
{
    Q_OBJECT
public:
    IntegerDataType(qint64 i = 0);
    IntegerDataType(const IntegerDataType& other);
    IntegerDataType& operator=(const IntegerDataType& other);

    ~IntegerDataType();

    qint64 toInt() const;

public slots:
    StringDataType* toStringDataType() const;
    QString* toString() const;

private:
    qint64 theInteger;
};
Q_DECLARE_METATYPE(IntegerDataType)
////////////////////////////////////////////////////////////////////////////

made factory:

////////////////////////////////////////////////////////////////////////////
class DataTypeFactory : public QSObjectFactory
{
public:
    DataTypeFactory();

    virtual QObject* create(const QString & name,
                            const QVariantList & arguments,
                            QObject * context );
};

DataTypeFactory::DataTypeFactory()
{
    qRegisterMetaType<IntegerDataType>("IntegerDataType");

    registerClass("IntegerDataType", &IntegerDataType::staticMetaObject);
}
////////////////////////////////////////////////////////////////////////////////



-- 
 [ signature omitted ] 

Message 2 in thread

I use Qsa 1.2.1

Maybe I should use QObject* instead of QVariant?

"Cepera" <serg1024@xxxxxxx> wrote in message 
news:e5115e$t5r$1@xxxxxxxxxxxxxxxxxxxxx
> What can I do to get custom user type value into C++ slot from Qsa?
>
> I want to use my custom DataTypes with Qsa and C++, like whis:
>
> /////////////////////////////
> class GlobalRegisterInterface : public QObject
> {
>    Q_OBJECT
> public:
>    GlobalRegisterInterface()
>    {
>          setObjectName("GRI");
>    }
>
> public slots:
>    QVariant* value(int valueId);
>    void setValue(int valueId, QVariant* value);
> };
>
> ....
>
> theQSProject->addObject(new GlobalRegisterInterface(theGlobalRegister));
> theQSProject->interpreter()->addObjectFactory(new DataTypeFactory());
>
> /////////////////////////////
> value integer: IntegerDataType;
>
> value = new IntegerDataType(300);
> GRI.setValue(200, value);
> value = GRI.value(100);
> /////////////////////////////
>
> But then I run program in GlobalRegisterInterface::setValue(...) I get 
> default constructered IntegerDataType, but not IntegerDataType(300).
>
>
>
> Some useful sources:
>
> /////////////////////////////////////////////////////////////////////////
> class IntegerDataType : public QObject
> {
>    Q_OBJECT
> public:
>    IntegerDataType(qint64 i = 0);
>    IntegerDataType(const IntegerDataType& other);
>    IntegerDataType& operator=(const IntegerDataType& other);
>
>    ~IntegerDataType();
>
>    qint64 toInt() const;
>
> public slots:
>    StringDataType* toStringDataType() const;
>    QString* toString() const;
>
> private:
>    qint64 theInteger;
> };
> Q_DECLARE_METATYPE(IntegerDataType)
> ////////////////////////////////////////////////////////////////////////////
>
> made factory:
>
> ////////////////////////////////////////////////////////////////////////////
> class DataTypeFactory : public QSObjectFactory
> {
> public:
>    DataTypeFactory();
>
>    virtual QObject* create(const QString & name,
>                            const QVariantList & arguments,
>                            QObject * context );
> };
>
> DataTypeFactory::DataTypeFactory()
> {
>    qRegisterMetaType<IntegerDataType>("IntegerDataType");
>
>    registerClass("IntegerDataType", &IntegerDataType::staticMetaObject);
> }
> ////////////////////////////////////////////////////////////////////////////////
>
>
>
> -- 
> Best regads,
> Sergey Pozdeyev 

To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx


Message 3 in thread

"Cepera" <serg1024@xxxxxxx> wrote in message 
news:e511i5$v2c$1@xxxxxxxxxxxxxxxxxxxxx
>I use Qsa 1.2.1
>
> Maybe I should use QObject* instead of QVariant?

QObject* works fine, but it usage is not so confortable as QVariant*. 
Because with QVariant I can store all data in simple QList<QVariant>, but if 
I use QObject* I can't to store in QList<QObject*> any of non QObject data 
(int, double and so on).

>
> "Cepera" <serg1024@xxxxxxx> wrote in message 
> news:e5115e$t5r$1@xxxxxxxxxxxxxxxxxxxxx
>> What can I do to get custom user type value into C++ slot from Qsa?
>>
>> I want to use my custom DataTypes with Qsa and C++, like whis:
>>
>> /////////////////////////////
>> class GlobalRegisterInterface : public QObject
>> {
>>    Q_OBJECT
>> public:
>>    GlobalRegisterInterface()
>>    {
>>          setObjectName("GRI");
>>    }
>>
>> public slots:
>>    QVariant* value(int valueId);
>>    void setValue(int valueId, QVariant* value);
>> };
>>
>> ....
>>
>> theQSProject->addObject(new GlobalRegisterInterface(theGlobalRegister));
>> theQSProject->interpreter()->addObjectFactory(new DataTypeFactory());
>>
>> /////////////////////////////
>> value integer: IntegerDataType;
>>
>> value = new IntegerDataType(300);
>> GRI.setValue(200, value);
>> value = GRI.value(100);
>> /////////////////////////////
>>
>> But then I run program in GlobalRegisterInterface::setValue(...) I get 
>> default constructered IntegerDataType, but not IntegerDataType(300).
>>
>>
>>
>> Some useful sources:
>>
>> /////////////////////////////////////////////////////////////////////////
>> class IntegerDataType : public QObject
>> {
>>    Q_OBJECT
>> public:
>>    IntegerDataType(qint64 i = 0);
>>    IntegerDataType(const IntegerDataType& other);
>>    IntegerDataType& operator=(const IntegerDataType& other);
>>
>>    ~IntegerDataType();
>>
>>    qint64 toInt() const;
>>
>> public slots:
>>    StringDataType* toStringDataType() const;
>>    QString* toString() const;
>>
>> private:
>>    qint64 theInteger;
>> };
>> Q_DECLARE_METATYPE(IntegerDataType)
>> ////////////////////////////////////////////////////////////////////////////
>>
>> made factory:
>>
>> ////////////////////////////////////////////////////////////////////////////
>> class DataTypeFactory : public QSObjectFactory
>> {
>> public:
>>    DataTypeFactory();
>>
>>    virtual QObject* create(const QString & name,
>>                            const QVariantList & arguments,
>>                            QObject * context );
>> };
>>
>> DataTypeFactory::DataTypeFactory()
>> {
>>    qRegisterMetaType<IntegerDataType>("IntegerDataType");
>>
>>    registerClass("IntegerDataType", &IntegerDataType::staticMetaObject);
>> }
>> ////////////////////////////////////////////////////////////////////////////////
>>
>>
>>
>> -- 
>> Best regads,
>> Sergey Pozdeyev
> 

To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx


Message 4 in thread

Cepera wrote:
> "Cepera" <serg1024@xxxxxxx> wrote in message 
> news:e511i5$v2c$1@xxxxxxxxxxxxxxxxxxxxx
> 
>> I use Qsa 1.2.1
>>
>> Maybe I should use QObject* instead of QVariant?
> 
> 
> QObject* works fine, but it usage is not so confortable as QVariant*. 
> Because with QVariant I can store all data in simple QList<QVariant>, 
> but if I use QObject* I can't to store in QList<QObject*> any of non 
> QObject data (int, double and so on).

Hi Cepera,

If you use QVariant as a value (not QVariant *) it should work just fine.

-
Gunnar

To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx


Message 5 in thread

"Gunnar Sletta" <gunnar@xxxxxxxxxxxxx> wrote in message 
news:44744C0F.6040106@xxxxxxxxxxxxxxxx
> Hi Cepera,
>
> If you use QVariant as a value (not QVariant *) it should work just fine.
>

Thanks for quick reply, I will try it.


-- 
 [ signature omitted ] 

Message 6 in thread

> Hi Cepera,
>
> If you use QVariant as a value (not QVariant *) it should work just fine.
>
> -
> Gunnar
>
> To unsubscribe - send "unsubscribe" in the subject to 
> qsa-interest-request@xxxxxxxxxxxxx

Now my class look like this:
////////////////////////////////////////////////////
class GlobalRegisterInterface : public QObject
{
    Q_OBJECT
public:
    GlobalRegisterInterface()
    {
          setObjectName("GRI");
    }

public slots:
    QVariant value(int valueId);
    void setValue(int valueId, QVariant value);
};
////////////////////////////////////////////////

And then I try to use script ASSERT happens:
//////////
var integer:IntegerDataType;
function func()
{
    integer = new IntegerDataType(100);

    GRI.setValue(0x1001, integer);
}
////////////

ASSERT failure in QVariant::construct(): "Unknown datatype", file 
kernel\qvariant.cpp, line 143

It happend then Qsa try to create QVariant for my custom type - 
IntegerDataType.
But I register my type for QVariant with
   Q_DECLARE_METATYPE(IntegerDataType)
   qRegisterMetaType<IntegerDataType>("IntegerDataType");
and add type into object factory - registerClass("IntegerDataType", 
&IntegerDataType::staticMetaObject);

With QObject* all works fine.


-- 
 [ signature omitted ] 

Message 7 in thread

Error happens in file quickobjects.cpp in function

QSObject qsa_execute_slot(QSEnv *env, QObject *qobject, const QList<int> 
&slot_indexes)

in line 1553

bool ok = qobject->qt_metacall(QMetaObject::InvokeMetaMethod, matched_slot, 
use_stored ? stored_data : data);


To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx


Message 8 in thread

I've found that I could NOT use registered meta types as QVariant  
objects by value within QSA.

I simply use the object pointers, (classes derived from QObject).

class A : public QObject
{
Q_OBJECT:
	// some stuff
};

class B : public QObject
{
	Q_OBJECT;
public slots:
	A *getA()
	{
		A *result = new A( myA );
		interpreter->addTransientObject( result, makeUniqueName(result) );
		return result;
	}
	void setA( A *a );
	{
		if( NULL != a )
		{
			myA = *a;
		}	
		return;
	}	
private
	A myA;
};

That works fine.. If you could use a QVariant type, you could use  
these user defined types as Q_PROPERTY attributes, which I found does  
NOT work.

brad


On May 25, 2006, at 4:28 AM, Cepera wrote:

> Error happens in file quickobjects.cpp in function
>
> QSObject qsa_execute_slot(QSEnv *env, QObject *qobject, const  
> QList<int> &slot_indexes)
>
> in line 1553
>
> bool ok = qobject->qt_metacall(QMetaObject::InvokeMetaMethod,  
> matched_slot, use_stored ? stored_data : data);
>
>
> To unsubscribe - send "unsubscribe" in the subject to qsa-interest- 
> request@xxxxxxxxxxxxx
>
>

To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx


Message 9 in thread

QVariant & QVariant doesn't work with custom registered user types.
Is it a bug or a feature?

QObject* works fine, but QVariant usage are more convenient.

"Cepera" <serg1024@xxxxxxx> wrote in message 
news:e53m0q$ghv$1@xxxxxxxxxxxxxxxxxxxxx
>> Hi Cepera,
>>
>> If you use QVariant as a value (not QVariant *) it should work just fine.
>>
>> -
>> Gunnar
>>
>> To unsubscribe - send "unsubscribe" in the subject to 
>> qsa-interest-request@xxxxxxxxxxxxx
>
> Now my class look like this:
> ////////////////////////////////////////////////////
> class GlobalRegisterInterface : public QObject
> {
>    Q_OBJECT
> public:
>    GlobalRegisterInterface()
>    {
>          setObjectName("GRI");
>    }
>
> public slots:
>    QVariant value(int valueId);
>    void setValue(int valueId, QVariant value);
> };
> ////////////////////////////////////////////////
>
> And then I try to use script ASSERT happens:
> //////////
> var integer:IntegerDataType;
> function func()
> {
>    integer = new IntegerDataType(100);
>
>    GRI.setValue(0x1001, integer);
> }
> ////////////
>
> ASSERT failure in QVariant::construct(): "Unknown datatype", file 
> kernel\qvariant.cpp, line 143
>
> It happend then Qsa try to create QVariant for my custom type - 
> IntegerDataType.
> But I register my type for QVariant with
>   Q_DECLARE_METATYPE(IntegerDataType)
>   qRegisterMetaType<IntegerDataType>("IntegerDataType");
> and add type into object factory - registerClass("IntegerDataType", 
> &IntegerDataType::staticMetaObject);
>
> With QObject* all works fine.
>
>
> -- 
> Best regads,
> Sergey Pozdeyev 

To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx


Message 10 in thread

Cepera wrote:
> QVariant & QVariant doesn't work with custom registered user types.
> Is it a bug or a feature?

This is a bug, we'll look into this for the next release.

-
Gunnar

To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx