Convert a QStandardItemModel to a QVariant

吃可爱长大的小学妹 提交于 2020-01-05 08:08:46

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/8279649/convert-a-qstandarditemmodel-to-a-qvariant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!