Jack
Jack

Reputation: 16724

How can I get value of QGenericArgument?

I have a QList of QGenericArgument in my application (to implement my event handlers class). In one specific case, I need to extract to value of an argument to do some checking. I know the type. So I wrote this:

template <class T>
T GetQArgumentValue(QGenericArgument &arg)
{
    const unsigned char* data = reinterpret_cast<const unsigned char*>(arg.data());
    return (T) *data;
}

And use like this:

QGenericArgument arg = Q_ARG(int, 30); // this is obviously an example, I get this from an array in real code
int value = GetQArgumentValue<int>(arg);

It works well. So my question are:

1) Since it's an internal helper class is there any issue of using it in my application?

2) Is there any built-in function to do this? I prefer built-in ones over re-invent the wheel.

edit:

What I want is this:

let's assume I have:

QGenericArgument arg = Q_ARG(int, 30);

I want to retrieve the 30 value back:

int v = magic_function<int>(arg); // v = 30

Upvotes: 0

Views: 722

Answers (1)

Orest Hera
Orest Hera

Reputation: 6776

In the Qt documentation it is not advised to use QGenericArgument in any way exept of creating it by Q_ARG for passing to QMetaObject::invokeMethod(). The reason is that the class QGenericArgument is actually a pair of two pointers (class members): void* for data and const char* for name.

It will work. However, it is similar to a list of void pointers and it is also bad in the same way.

If you pass such list to any function you should take care about life time of those objects: original objects pointed by those pointers should be still valid for enough time and also it is tricky with desruction of such objects.

If you need to manage some dynamic list of arguments before passing them to QMetaMethod it is safer and nicer to use some wrapper for those arguments. For example, there is the class QVariant in Qt. So, QList of QVariant or QVariantMap can be used here.

Upvotes: 3

Related Questions