为了扩展TreeView的节点以显示当前选择的项目(由共享选择模型设置),我需要递归调用TreeView.expand(QModelIndex)。
expand(index)
expand(index.parent)
expand(index.parent.parent)
...
这可以通过这样的函数完成:
function autoExpand(index ) {
print(index)
var oneUp = index
do {
print(oneUp)
oneUp = index.parent
expand(oneUp)
print("do")
print(oneUp)
} while (oneUp)
}
但我不知道如何检查根节点。我试过了
while (oneUp) -> always true
while (oneUp.isValid) -> undefined ie always false
while (oneUp.isValid()) -> property isValid cannot be called as a function
,在c ++中它将是:
do {
//....
} while (oneUp.isValid());
但我在QML中找不到相同的功能(并且不知道在哪里查找代码......)
作为一种解决方法,我在已经导出的对象中用c ++检查它,但是它看起来不合适:
public slots:
bool indexIsValid(const QModelIndex &index) const {return index.isValid();}
答案 0 :(得分:2)
作为zizix复制粘贴答案的副作用,我了解到QModelIndex :: isValid函数转换为QML中的index.valid属性(?)。使用它我现在可以在QML中成功检测到树的根:
function autoExpand(index ) {
var oneUp = index
do {
oneUp = oneUp.parent
expand(oneUp)
} while (oneUp.valid);
}
答案 1 :(得分:1)
“... \ Qt的\ 5.5 \ SRC \ qtquickcontrols \ SRC \控制\ TreeView.qml”
private void callWebservice() {
String tag_json_obj = "GetItem";
String url = String.format("http://digitalbox.getsandbox.com/item");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
saveSections(response.getJSONArray("Sections"));
// SELECT * FROM Sections where Fk == 0;
sectionsList = new RushSearch().whereEqual("Fk", 0).find(Sections.class);
Log.d("ELEMENTI",""+sectionsList.size());
drawerListViewAdapter = new DrawerListViewAdapter(sectionsList, getApplicationContext());
// Adapter che si occupa di popolare il drawer
drawerListView.setAdapter(drawerListViewAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Settings", "Error: " + error.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Accept", "application/json");
return headers;
}
};
NetworkController.getInstance().addToRequestQueue(jsonObjectRequest, tag_json_obj);