我有一个继承自 QObject 的 Transform 类的 Light 类。 Transform 类有一个 center() 函数,它返回一个 Point3(QVector3D 的 typedef)。我试图在轻量对象上调用 invokeMethod,但得到的返回值不正确。我是否错误地调用了该函数?
QObject* obj = qobject_cast<QObject*>(lObj);
std::cout << lObj->center() << std::endl;
QVector3D retVal;
QMetaObject::invokeMethod(obj, "center",
Qt::DirectConnection,
Q_RETURN_ARG(QVector3D, retVal));
std::cout << retVal << std::endl;
这就是我得到的结果......
(0,10,0) // from lObj->center()
(0,0,0) // from invokeMethod(...)
这是我的一些代码...
typedef QVector3D Point3;
class Transformable : public QObject
{
Q_OBJECT
Q_INVOKABLE Point3 center() { return _center; }
...
};
class Light : public Transformable, public Entity // Entity is a non-Qt-related class
{
};
class PointLight : public Light
{
};
最佳答案
使用 qRegisterMetaType 注册 Point3 typedef。
typedef QVector3D Point3;
qRegisterMetaType<Point3>("Point3");
然后在调用方法中使用此类型而不是 QVector3D
Point3 retVal;
QMetaObject::invokeMethod(obj, "center",
Qt::DirectConnection,
Q_RETURN_ARG(Point3, retVal));
有关更多详细信息,请查看invoke method documentation和 qRegisterMetaType documentation .
关于c++ - qt:继承父类(super class)的 invokemethod 返回值不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7911292/