Qt那些事0.0.28 之 QML中的C++對象

今天脫了個坑,很大的坑。

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,解決問題。

------------------------------

得知喫播中的泡泡龍過世,對肥胖又平添了恐懼。鍛鍊吧,少年!

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章