Hi ,
i'm working with qt 4.8.
I have my model in c++ and a ListView in Qml.
I set my model in c++ like this
{
Q_OBJECT
public:
enum ListModelRole
{
rName = Qt::UserRole + 1,
rId = Qt::UserRole + 1,
};
virtual ~ListModel ();
virtual void ClearWorklistModel();
void SetValues( const vector<pair<string, string>> & iValues );
protected :
vector<pair<string, string>> m_values ;
};
ListModel : public QAbstractListModel
{
Q_OBJECT
public:
enum ListModelRole
{
rName = Qt::UserRole + 1,
rId = Qt::UserRole + 1,
};
ListModel (QObject *parent = 0);
virtual ~ListModel ();
virtual void ClearWorklistModel();
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
void SetValues( const vector<pair<string, string>> & iValues );
protected :
vector<pair<string, string>> m_values ;
};
To copy to clipboard, switch view to plain text mode
ListModel
::ListModel( QObject *pParent
/*NULL*/ ){
QHash<int, QByteArray> roles;
roles[rName ] = "rName";
roles[rId ] = "rId";
setRoleNames( roles );
}
int
ListModel
::rowCount( const QModelIndex & parent
) const{
return m_values.size();
}
void
ListModel::ClearWorklistModel()
{
beginRemoveRows( startRefreshIndex, 0, m_values.size()-1 );
this->removeRows( 0, m_values.size()-1 );
endRemoveRows();
m_values.clear();
}
void
ListModel::SetValues( const vector<pair<string, string>> & iValues )
{
this->ClearWorklistModel();
this->beginResetModel();
m_values = iValues ;
this->endResetModel();
}
ListModel
::data( const QModelIndex & index,
int role
) const{
// check conditions
if (index.row() < 0 || (size_t)index.row() > m_values.size() )
{
}
switch( role )
{
case rName :
value = m_values.at( index.row() ).first.c_str() ;
break;
case rId :
value = m_values.at( index.row() ).second.c_str() ;
break;
}
return value ;
}
ListModel::ListModel( QObject *pParent/*NULL*/ )
: QAbstractListModel(pParent)
{
QHash<int, QByteArray> roles;
roles[rName ] = "rName";
roles[rId ] = "rId";
setRoleNames( roles );
}
int
ListModel::rowCount( const QModelIndex & parent ) const
{
return m_values.size();
}
void
ListModel::ClearWorklistModel()
{
beginRemoveRows( startRefreshIndex, 0, m_values.size()-1 );
this->removeRows( 0, m_values.size()-1 );
endRemoveRows();
m_values.clear();
}
void
ListModel::SetValues( const vector<pair<string, string>> & iValues )
{
this->ClearWorklistModel();
this->beginResetModel();
m_values = iValues ;
this->endResetModel();
}
QVariant
ListModel::data( const QModelIndex & index, int role ) const
{
// check conditions
if (index.row() < 0 || (size_t)index.row() > m_values.size() )
{
return QVariant();
}
QVariant value ;
switch( role )
{
case rName :
value = m_values.at( index.row() ).first.c_str() ;
break;
case rId :
value = m_values.at( index.row() ).second.c_str() ;
break;
}
return value ;
}
To copy to clipboard, switch view to plain text mode
and my ListView
ListView
{
id : myListView
objectName : "myWorklistView"
model: myModel
delegate: Item
{
Text
{
test : rName + " _ " + rId
}
Image
{
source: "ClientThumbnail.png"
}
}
}
ListView
{
id : myListView
objectName : "myWorklistView"
model: myModel
delegate: Item
{
Text
{
test : rName + " _ " + rId
}
Image
{
source: "ClientThumbnail.png"
}
}
}
To copy to clipboard, switch view to plain text mode
So in my program i create a QDeclarativeView and then i set the model like
QDeclarativeView
*pDeclarativeListView
= new QDeclarativeView
( QUrl("myqmlFile.qml") );
QDeclarativeContext * pDeclarativeCtxt = pDeclarativeListView->rootContext();
pDeclarativeCtxt->setContextProperty( "myModel", ¤tWorkListModel );
QDeclarativeView *pDeclarativeListView = new QDeclarativeView( QUrl("myqmlFile.qml") );
QDeclarativeContext * pDeclarativeCtxt = pDeclarativeListView->rootContext();
pDeclarativeCtxt->setContextProperty( "myModel", ¤tWorkListModel );
To copy to clipboard, switch view to plain text mode
At last i want to refresh my ListView i call SetValues but each time i set new Values i have some memory consumption. I notice when i delete my DeclarativeView the memory consumption is fine.
I imagine there is some Garbage Collector in QML that handle QDeclarativeItems created but how can I tell to QML that i want that items which are no longer available in my ListView have to be deleted.
Thanks in advance
Pierre
Bookmarks