在下面的代码中,我在C ++中创建MarketListModel(从QObject派生)并在QML中使用它:
class ExchangeListModel : public QAbstractListModel
{
Q_OBJECT
public:
Q_INVOKABLE MarketListModel * getMarkets(int index);
};
MarketListModel * ExchangeListModel::getMarkets(int index)
{
//...
return new MarketListModel(this);
}
QML:
ListView {
id: exchange_list
Layout.fillWidth: true
height: parent.height / 4
model: model
onSelected: {
log("%1 exchange index selected".arg(index))
//drawer.close()
market_list.model = model.getMarkets(index)
}
此代码是否会产生内存泄漏?如果是,是否可以销毁QML中的对象?如果我创建的父对象为空怎么办?
return new MarketListModel(nullptr);
?