我尝试重新实现filterAcceptsRow(int source_row, const QModelIndex &source_parent)-method of a QSortFilterProxyModel。
在这里,我想调用一个可调用的QJSValue,并将这两个参数传递给它。为此,我需要将它们放在QJSValueList中,这对于整数来说很容易。
但是,我无法找到对QModelIndex进行同样处理的方法。
JSValue没有接受QModelIndex的构造函数。QJSEngine::createQObject拿QObject,我没有。我有成功的机会吗?
编辑:我现在尝试了什么,
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);
}请原谅我任何琐碎的错误。自从我上次使用(并开始学习) C++以来已经有5年了。
发布于 2017-01-11 20:03:56
根据您的描述,我不太清楚您在做什么,但是假设您有一个QModelIndex (在C++中),并且出于某种原因想要一个QJSValue,那么我认为QJSEngine::toScriptValue确实是您想要的。这可能应该从QJSValue文档中链接,我将在以后的发行版中尝试修复它。
我自己从来没有这样做过,但是像这样的事情应该能起作用:
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!如果您想走相反的方向,即从一个QModelIndex解封一个QJSValue:
QJSValue val = something->prop(); // now to extract it...
QModelIndex m = e->fromScriptValue<QModelIndex>(val);
// go ahead and use m!QVariant和QJSValue都是动态的“框”,可以包含不同类型的数据,因此您不希望加倍使用QJSValue(QVariant(QModelIndex)) (不会编译,只是为了演示存储),因为它不知道如何正确地解压缩它。
https://stackoverflow.com/questions/41592001
复制相似问题