![](/img/trans.png)
[英]Send callback as argument to QJSValue::callAsConstructor()
[英]QModelIndex to QJSValue
我試圖重新實現filterAcceptsRow(int source_row, const QModelIndex &source_parent)
一個的-方法QSortFilterProxyModel
。
在這里,我想調用可調用的QJSValue
並將兩個參數傳遞給它。 為此,我需要將它們放在QJSValueList
,這對於整數很容易。
但是我找不到與QModelIndex
相同的QModelIndex
。
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年了。
我不太確定您從描述中正在做什么,但是假設您有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!
如果要走另一個方向,也就是從QJSValue中拆開QModelIndex:
QJSValue val = something->prop(); // now to extract it...
QModelIndex m = e->fromScriptValue<QModelIndex>(val);
// go ahead and use m!
QVariant
和QJSValue
都是動態的“盒子”,可以包含不同類型的數據,因此您不希望加倍並給QML等於QJSValue(QVariant(QModelIndex))
(不會編譯,只是為了演示存儲空間),因為它不知道如何正確拆箱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.