今天脫了個坑,很大的坑。
C++代碼中建立的類,在QML通過Q_INVOKABLE的函數獲取此類一個對象的指針,作爲一個Repeater的model。
如:
Column{
Repeater{
model:$core.getModel(0)
delegate:Rectangle{}
}
}
class Model:public QAbstractListModel{
}
class Core:public QObject{
public:
Core(QObject* parent = nullptr):QObject(parent){
for(int i = 0;i < 4;i++){
m_models.append(new Model);
}
}
Model* getModel(int index){return m_models.at(index);}
private:
QList<Model*> m_models;
}
測試程序運行沒有問題,可移植到主程序就崩潰。最後測試出爲m_models中的對象被釋放(增加析構函數後打印消息)。
猜測是構造Model時沒有指定父對象,測試修改Model,將Core對象作爲Model的parent,解決問題。
------------------------------
得知喫播中的泡泡龍過世,對肥胖又平添了恐懼。鍛鍊吧,少年!