将QStandardItemModel转换为QVariant [英] Convert a QStandardItemModel to a QVariant

查看:353
本文介绍了将QStandardItemModel转换为QVariant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将QStandardItemModel派生的对象发送到PythonQt,但是我对如何发送它有点困惑.当我使用boost :: python时,我有几个控件,例如boost :: noncopyable,以确保我没有重新创建该对象,而是与python共享.我还具有从python内部提供指向python的增强共享指针的构造.

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

但是,在PythonQt中,我不确定有什么可用.函数call的所有函数参数均使用QVariantList.

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))

我现在很困惑的是如何通过QVariant将对象传递给python.由于它是从QStandardItemModel派生的,所以我认为它已经是寄存器

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

但这给了我以下错误:

'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.  

我实际上得到了两次错误-一次是在添加Q_DECLARE_METATYPE的标头中,另一个是在标头中,该标头始终从QStandardItemModel派生,但在其他情况下则无关.

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.

Q_DECLARE_METATYPE甚至是将该对象转换为QVariant的正确方法吗?

Is Q_DECLARE_METATYPE even the correct way to go about converting this object to a QVariant?

BOOST_PYTHON_MODULE(场景) { class _("Scene"); }

BOOST_PYTHON_MODULE(scene) { class_("Scene"); }

推荐答案

是的,默认情况下,QVariant可以采用以下类型之一- 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 call qRegisterMetaType() function.

这篇关于将QStandardItemModel转换为QVariant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆