A
A
Alexander2015-08-06 17:31:36
Qt
Alexander, 2015-08-06 17:31:36

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(); 
  }
}

What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander, 2015-08-06
@wxmaper

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); 
        /* ... */
    }
}

works.

M
maaGames, 2015-08-06
@maaGames

QGenericReturnArgument returnArgument(metaMethod.typeName(), &data)
replacement with
QGenericReturnArgument returnArgument(metaMethod.typeName(), *data)

J
jcmvbkbc, 2015-08-06
@jcmvbkbc

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;
}

Those. your mistake is that instead of the data passed from somefunction, you are using a local copy of the data from call.

A
Andrey Burov, 2015-08-06
@BuriK666

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 question

Ask a Question

731 491 924 answers to any question