[英]QModelIndex to QJSValue
I try to reimplement the filterAcceptsRow(int source_row, const QModelIndex &source_parent)
-method of a QSortFilterProxyModel
. 我试图重新实现
filterAcceptsRow(int source_row, const QModelIndex &source_parent)
一个的-方法QSortFilterProxyModel
。
Here I want to call a callable QJSValue
and pass the two parameters to it. 在这里,我想调用可调用的
QJSValue
并将两个参数传递给它。 For this I need to have them in a QJSValueList
, which is easy for the integer. 为此,我需要将它们放在
QJSValueList
,这对于整数很容易。
But I fail to find the method to do the same with the QModelIndex
. 但是我找不到与
QModelIndex
相同的QModelIndex
。
JSValue
that takes a QModelIndex
QModelIndex
的JSValue
构造QModelIndex
QJSEngine::createQObject
takes a QObject, which I don't have. QJSEngine::createQObject
接受一个我没有的QObject。 Do I have any chance on succeeding? 我有成功的机会吗?
EDIT: WHAT I TRIED NOW 编辑:我现在尝试过的
bool FilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
if (m_filter.isCallable()) { // check whether a JS-Function is set
QJSValueList l;
QJSValue f = QJSValue(m_filter);
l << QJSValue(source_row);
// **** ADD MORE USEFULL STUFF HERE ****
// This is working now - thanks to your help. But useless in QML
QJSEngine *engine = m_filter.engine();
l << engine->toScriptValue(source_parent);// <-- Value is of no use in QML. Can't do anything with it. And for ListModels as source utterly useless
// V----- To add this would make more sense, but the app crashes. Don't call index()!!!
// l << engine->toScriptValue(index(source_row, 0, source_parent));
return f.call(l).toBool();
}
// If no JS-Function is set, fall back to the original method
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
Please forgive me any trivial errors. 请原谅我任何小错误。 Its 5 years since I last used (and started to learn) C++.
自从我上次使用(并开始学习)C ++已有5年了。
I'm not quite sure what you're doing from your description, but assuming you have a QModelIndex
(in C++) and want a QJSValue
for some reason, then I think QJSEngine::toScriptValue
is indeed what you want. 我不太确定您从描述中正在做什么,但是假设您有
QModelIndex
(在C ++中)并且出于某种原因想要QJSValue
,那么我认为QJSEngine::toScriptValue
确实是您想要的。 This should probably be linked from the QJSValue
doc, I'll try fix that for a future release. 这可能应该与
QJSValue
文档链接,我将在以后的版本中尝试解决该问题。
I've never done this myself, but something like this should work: 我自己从来没有做过,但是这样的事情应该起作用:
QJSEngine *e; /* I assume you've got this already somewhere .. */
QModelIndex m = something->index(...); /* and you have a model index */
QJSValue val = e->toScriptValue(m);
// go ahead and use val!
If you want to go the other direction, that is, unboxing a QModelIndex from a QJSValue: 如果要走另一个方向,也就是从QJSValue中拆开QModelIndex:
QJSValue val = something->prop(); // now to extract it...
QModelIndex m = e->fromScriptValue<QModelIndex>(val);
// go ahead and use m!
Both QVariant
and QJSValue
are sort of dynamic "boxes" that can contain different types of data, so you don't want to double up and give QML what amounts to QJSValue(QVariant(QModelIndex))
(won't compile, just to demonstrate storage), because it won't know how to unbox it properly. QVariant
和QJSValue
都是动态的“盒子”,可以包含不同类型的数据,因此您不希望加倍并给QML等于QJSValue(QVariant(QModelIndex))
(不会编译,只是为了演示存储空间),因为它不知道如何正确拆箱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.