将 QStandardItemModel 转换为 QVariant
我正在尝试将 QStandardItemModel 派生的对象发送到 PythonQt,但我对如何发送它有点困惑。当我使用 boost::python 时,我有几个控件,如 boost::noncopyable 来确保我没有重新创建这个对象,而是与 python 共享它。我还有一些构造可以从 python 内部提供一个指向 python 的 boost 共享指针。
class Scene : public boost::enable_shared_from_this
但是,在 PythonQt 中,我不确定有什么可用。函数call
采用QVariantList作为所有函数参数。
QVariant PythonQt::call(PyObject* object, const QString &callable, const QVariantList &args = QVariantList))
我现在困惑的是如何通过 QVariant 将我的对象获取到 python 。由于它派生自 QStandardItemModel,我认为它已经被注册了
void MyObject::someFunction(QString fileName)
{
QVariant myObjectV = qVariantFromValue(this);
// send to python
...
}
但这给了我以下错误:
'qt_metatype_id' : is not a member of 'QMetaTypeId
我尝试在之后注册它我声明了我的类,但这会引发不同的错误。
class MyObject : public QStandardItemModel
{
Q_OBJECT
...
};
Q_DECLARE_METATYPE(MyObject)
QStandardItemModel::QStandardItemModel(const QStandardItemModel&) is private within this context.
实际上,我收到了两次错误 - 一次是在标头中添加了 Q_DECLARE_METATYPE ,一次是在另一个标头中,该标头有一个始终派生自 QStandardItemModel 但在其他方面不相关的类。
Q_DECLARE_METATYPE 是将这个对象转换为 QVariant 的正确方法吗?
BOOST_PYTHON_MODULE(场景) { class_(“场景”); }
I'm trying to send a QStandardItemModel-derived object to PythonQt, but I'm a little confused on how it needs to be sent. When I was using boost::python I had several controls like boost::noncopyable to ensure I wasn't recreating this object, but sharing it with python. I also had constructs to provide a boost shared pointer to python from inside python.
class Scene : public boost::enable_shared_from_this<Scene>, public QStandardItemModel
In PythonQt, however, I'm not sure what's available. The function call
takes a QVariantList for all the function parameters.
QVariant PythonQt::call(PyObject* object, const QString &callable, const QVariantList &args = QVariantList))
What I'm confused about now is how to get my object to python via a QVariant. Since its derived from QStandardItemModel, I figured it would already be register
void MyObject::someFunction(QString fileName)
{
QVariant myObjectV = qVariantFromValue(this);
// send to python
...
}
But this gives me the following error:
'qt_metatype_id' : is not a member of 'QMetaTypeId<MyObject>'
I've tried registering it after I declare my class, but this throws a different error.
class MyObject : public QStandardItemModel
{
Q_OBJECT
...
};
Q_DECLARE_METATYPE(MyObject)
QStandardItemModel::QStandardItemModel(const QStandardItemModel&) is private within this context.
I actually get the error twice--once in header where I add the Q_DECLARE_METATYPE and in another header, which has a class which always derives from QStandardItemModel but is otherwise unrelated.
Is Q_DECLARE_METATYPE even the correct way to go about converting this object to a QVariant?
BOOST_PYTHON_MODULE(scene)
{
class_("Scene");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,默认情况下,
QVariant
可以采用以下类型之一 - http://doc.qt.io/qt-4.8/qvariant.html#Type-enum - 它们不足以完成您的任务。您应该通过 qmetatype 系统自行声明其他类型。因此你应该调用qRegisterMetaType()
函数。Yes, by default,
QVariant
can take one of te following types - http://doc.qt.io/qt-4.8/qvariant.html#Type-enum - and they are not enough for your task. You should declare additional types by yourself via qmetatype system. Thus you shoud callqRegisterMetaType()
function.