qml之invokeMethod的使用

413 阅读1分钟



[static]bool QMetaObject::invokeMethod(QObject *obj, constchar *member,Qt::ConnectionTypetype,QGenericReturnArgumentret,QGenericArgumentval0 = QGenericArgument( Q_NULLPTR ),

调用成员(信号或槽的名称)的对象。如果成员可以被调用,返回true。如果没有这样的成员或参数不匹配,则返回false。

调用可以是同步的,也可以是异步的,这取决于类型:
如果类型是QT::直接,成员将立即调用。

\

QMetaObject::invokeMethod(thread, "quit",Qt::QueuedConnection);

 QString retVal;

  QMetaObject::invokeMethod(obj, "compute", Qt::DirectConnection,
                            Q_RETURN_ARG(QString, retVal),
                            Q_ARG(QString, "sqrt"),
                            Q_ARG(int, 42),
                            Q_ARG(double, 9.7));

\

\

\

qml中写了一个   js函数                           

functiongetItem(index)

{
  return idlistElemnet.get(index);
 }

c++中通过这样来调用

QObject*topLevel=engine.rootObjects().value(0);

    QQuickWindow *window=qobject_cast<QQuickWindow*>(topLevel);
    QObject *model=window->findChild<QObject*>("dataModel");
    QVariant retValue;
    bool result;
        QMetaObject::invokeMethod(model,"getItem",Qt::DirectConnection,

                              Q_RETURN_ARG(QVariant,retValue),

                              Q_ARG(QVariant,QVariant::fromValue(0)));

\

\

\

\