Proxy für TreeModel

Alles rund um die Programmierung mit Qt
Antworten
patrikD
Beiträge: 22
Registriert: 21. Mai 2007 14:01

Proxy für TreeModel

Beitrag von patrikD »

Hallo zusammen,

ich möchte für das SimpleTreeModel aus den Beispielen ein ProxyModel basteln. Aber irgendwie bekomm ich die Parent-Beziehung der ModelIndexe im Proxy nicht hin. Die erste Hierarchieebene unterhalb des Root wird richtig umgesetzt aber dann klappt das Indizieren über den Parent nicht. Das Model bekommt ja seine Parentbeziehung über das gespeicherte Item, aber wenn ich das richtig verstanden habe greift man mit dem Proxy nicht auf die Items zu sondern macht alles über den Index.

Code: Alles auswählen

QModelIndex CUserProxyNoMeta::index(int r, int c, const QModelIndex& parent) const
{
	if (!hasIndex(r, c))
        return QModelIndex();
	return createIndex(r, c);
}

QModelIndex CUserProxyNoMeta::mapFromSource(const QModelIndex& sourceIndex) const
{
	if (!sourceIndex.isValid()){
		return QModelIndex();
	}

	return index(sourceIndex.row(), sourceIndex.column());
}

QModelIndex CUserProxyNoMeta::mapToSource(const QModelIndex& proxyIndex) const
{
	if (!proxyIndex.isValid()){
		return QModelIndex();
	}
	return sourceModel()->index(proxyIndex.row(), proxyIndex.column());
}

QVariant CUserProxyNoMeta::data(const QModelIndex& ind, int role) const
{
	return sourceModel()->data(mapToSource(ind), role);
}

QModelIndex CUserProxyNoMeta::parent(const QModelIndex& index) const 
{
	if (!index.isValid()){
		return QModelIndex();
	}
	return mapFromSource(sourceModel()->parent(mapToSource(index)));
}

int CUserProxyNoMeta::rowCount(const QModelIndex& ind) const{
	return sourceModel()->rowCount(mapToSource(ind));
}

int CUserProxyNoMeta::columnCount(const QModelIndex& ind) const
{
	return sourceModel()->columnCount(mapToSource(ind));
}
Was mach ich falsch?

lg
bensen
Beiträge: 4
Registriert: 8. Januar 2008 12:43

Beitrag von bensen »

Hallo,

wozu so umständlich ? Was willst du mit dem ProxyModel ? Normalerweise wird's zum Sortieren und Filtern verwendet.
Für die Implementation reicht das Überschreiben der protected Methoden aus, z.B. für die Sortierung :

Code: Alles auswählen

bool MeinProxyModel::lessThan( const QModelIndex & left, const QModelIndex & right ) const

Du musst dann nur deine eigentlichen referenzierten Daten aus den Indize abfragen und vergleichen, den Rest erledigt QT.
Antworten