Answer the question
In order to leave comments, you need to log in
How to properly pass void to a function (C++/Qt)?
There is such a code. As an example, I'm trying to call the layout() metamethod on a QWidget descendant (Q_INVOKABLE is set).
In the call() function everything works fine, but in somefunction() the *data variable comes broken or empty:
bool call(QObject *qo, QMetaMethod metaMethod, void **data) {
QGenericReturnArgument returnArgument(metaMethod.typeName(), &data);
bool ok = metaMethod.invoke(qo, Qt::DirectConnection, returnArgument);
if(ok) {
QLayout *layout = reinterpret_cast<QLayout *>(data); // Всё ок
qDebug() << layout ->metaObject()->className(); // QLayout
}
return ok;
}
void somefunction() {
/* ... */
void *data;
bool ok = call( qo, metaMethod, &data );
if(ok) {
QLayout *layout = reinterpret_cast<QLayout *>(data); // Segfault
qDebug() << layout ->metaObject()->className();
}
}
Answer the question
In order to leave comments, you need to log in
Briefly did this:
bool call(QWidget *qo, QMetaMethod metaMethod, QGenericReturnArgument *returnArgument)
{
/* ... */
return metaMethod.invoke(qo, *returnArgument);
}
void somefunction()
{
void *vlayout;
QGenericReturnArgument returnArgument(metaMethod.typeName(), &vlayout);
if( call(qo, metaMethod, &returnArgument) ) {
QLayout *layout = reinterpret_cast<QLayout*>(vlayout);
/* ... */
}
}
QGenericReturnArgument returnArgument(metaMethod.typeName(), &data)
replacement with
QGenericReturnArgument returnArgument(metaMethod.typeName(), *data)
I think it should be like this:
bool call(QObject *qo, QMetaMethod metaMethod, void **data) {
QGenericReturnArgument returnArgument(metaMethod.typeName(), data);
bool ok = metaMethod.invoke(qo, Qt::DirectConnection, returnArgument);
if(ok) {
QLayout *layout = reinterpret_cast<QLayout *>(*data); // Всё ок
qDebug() << layout ->metaObject()->className(); // QLayout
}
return ok;
}
Inside call
data you have type void **
and inside somefunction - void *
QLayout *layout = reinterpret_cast<QLayout *>(&data); // Segfault
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question