20200209-01 QML TableView 異常釋放 C++ 對象

在 TableView 中用這種方式賦值,似乎將 C++ 對象與 QML 對象綁定在一起了,所以會被自動釋放掉

問題碼

Component.onCompleted: {
    var work = tableView.model.getItem(styleData.row)
    work.onValueChanged.connect(setText)
    text = work.mCurrentValue
}

function setText(val) { 
    text = val
}

Note: Avoid storing any state inside a delegate. If you do, reset it manually on receiving the TableView::reused signal.

If an item has timers or animations, consider pausing them on receiving the TableView::pooled signal. That way you avoid using the CPU resources for items that are not visible. Likewise, if an item has resources that cannot be reused, they could be freed up.

If you don't want to reuse items or if the delegate cannot support it, you can set the reuseItems property to false.

看這段的意思就是,對於那些不可以重複的內容, TableView 自帶的 reused 機制可能會將其釋放掉,需要顯式聲明 false

Reusing items


我的處理方案

property var workObj: tableView.model.getItem(styleData.row)
text: workObjT !== null ? workObjT.mCurrentValue : ""

用這種方式代替

原因:

沒有在 import QtQuick.Controls 1.4 版本中找到相關描述

發佈了125 篇原創文章 · 獲贊 27 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章