Proxy model example code

From Qt Wiki
Revision as of 09:28, 24 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search



Related to "this FAQ entry":http://developer.qt.nokia.com/faq/answer/how_can_i_implement_my_own_proxy_model_that_is_more_advanced_than_just_sort.

<br />#include &lt;QtGui&amp;gt;

QList&amp;lt;QStandardItem *&gt; list;

class SortProxy : public QAbstractProxyModel<br />{<br /> Q_OBJECT

public:<br /> SortProxy(QObject *parent = 0) : QAbstractProxyModel(parent), hideThem(false)<br /> {<br /> fixModel();<br /> }

int rowCount(const QModelIndex &amp;parent) const<br /> {<br /> QModelIndex sourceParent;<br /> if (parent.isValid())<br /> sourceParent = mapToSource(parent);<br /> int count = 0;<br /> QMapIterator&amp;lt;QPersistentModelIndex, QPersistentModelIndex&amp;gt; it(proxySourceParent);<br /> while (it.hasNext()) {<br /> it.next();<br /> if (it.value() == sourceParent)<br /> count++;<br /> }<br /> return count;<br /> }

int columnCount(const QModelIndex &amp;) const<br /> {<br /> return 1;<br /> }

QModelIndex index(int row, int column, const QModelIndex &amp;parent = QModelIndex()) const<br /> {<br /> QModelIndex sourceParent;<br /> if (parent.isValid())<br /> sourceParent = mapToSource(parent);<br /> QMapIterator&amp;lt;QPersistentModelIndex, QPersistentModelIndex&amp;gt; it(proxySourceParent);<br /> while (it.hasNext()) {<br /> it.next();<br /> if (it.value()  sourceParent &amp;amp;&amp;amp; it.key().row()  row &amp;&amp;<br /> it.key().column() == column)<br /> return it.key();<br /> }<br /> return QModelIndex();<br /> }

QModelIndex parent(const QModelIndex &amp;child) const<br /> {<br /> QModelIndex mi = proxySourceParent.value(child);<br /> if (mi.isValid())<br /> return mapFromSource(mi);<br /> return QModelIndex();<br /> }

QModelIndex mapToSource(const QModelIndex &amp;proxyIndex) const<br /> {<br /> if (!proxyIndex.isValid())<br /> return QModelIndex();<br /> return mapping.key(proxyIndex);<br /> }

QModelIndex mapFromSource(const QModelIndex &amp;sourceIndex) const<br /> {<br /> if (!sourceIndex.isValid())<br /> return QModelIndex();<br /> return mapping.value(sourceIndex);<br /> }

public slots:<br /> void hideEverythingButA1AndChildren()<br /> {<br /> hideThem = !hideThem;<br /> // Now we set up the proxy &lt;<s>&gt; source mappings<br /> emit layoutAboutToBeChanged();<br /> fixModel();<br /> emit layoutChanged();<br /> }
<br />private:<br /> void fixModel()<br /> {<br /> mapping.clear();<br /> proxySourceParent.clear();<br /> for (int i=0;i&amp;lt;list.size();i+'') {<br /> QStandardItem *si = list.at(i);<br /> if (hideThem) {<br /> if (!si-&gt;text().startsWith(&quot;A&amp;quot;) || !si-&gt;parent())<br /> continue;<br /> QModelIndex proxy = createIndex(si-&gt;row(), si-&gt;column(), si-&gt;index().internalPointer());<br /> mapping.insert(QPersistentModelIndex(si-&gt;index()), proxy);<br /> QModelIndex sourceParent;<br /> if (si-&gt;parent()<s>&gt;parent())<br /> sourceParent = si</s>&gt;parent()<s>&gt;index();<br /> proxySourceParent.insert(proxy, sourceParent);<br /> } else {<br /> QModelIndex proxy = createIndex(si</s>&gt;row(), si-&gt;column(), si-&gt;index().internalPointer());<br /> mapping.insert(QPersistentModelIndex(si-&gt;index()), proxy);<br /> QModelIndex sourceParent;<br /> if (si-&gt;parent())<br /> sourceParent = si-&gt;parent()<s>&gt;index();<br /> proxySourceParent.insert(proxy, sourceParent);<br /> }<br /> }<br /> }<br /> QMap&amp;lt;QPersistentModelIndex, QPersistentModelIndex&amp;gt; mapping;<br /> QMap&amp;lt;QPersistentModelIndex, QPersistentModelIndex&amp;gt; proxySourceParent;<br /> bool hideThem;<br />};
<br />SortProxy *proxyModel = 0;
<br />class Tree : public QTreeView<br />{<br /> Q_OBJECT
<br />public:<br /> Tree(QWidget *parent = 0) : QTreeView(parent)<br /> {<br /> QStandardItemModel *sourceModel = new QStandardItemModel(this);
<br /> QStandardItem *parentA = sourceModel</s>&gt;invisibleRootItem();<br /> for (int i = 0; i &lt; 2;i) {<br /> itemA = new QStandardItem(QString(&quot;A %0&amp;quot;).arg(i));<br /> parentA-&gt;appendRow(itemA);<br /> parentA = itemA;<br /> list.append(itemA);<br /> }<br /> itemA = new QStandardItem(QString(&quot;A 2&amp;quot;));<br /> parentA-&gt;appendRow(itemA);<br /> list.append(itemA);<br /> itemA3 = new QStandardItem(QString(&quot;A 3&amp;quot;));<br /> list.append(itemA3);<br /> parentA-&gt;appendRow(itemA3);<br /> itemA4 = new QStandardItem(QString(&quot;A 4&amp;quot;));<br /> list.append(itemA4);<br /> parentA-&gt;appendRow(itemA4);<br /> itemNonA = new QStandardItem(QString(&quot;Non A&amp;quot;));<br /> list.append(itemNonA);<br /> parentA-&gt;appendRow(itemNonA);
<br /> QStandardItem *parentB = sourceModel-&gt;invisibleRootItem();<br /> for (int i = 0; i &lt; 3;i) {<br /> itemB = new QStandardItem(QString(&quot;B %0&amp;quot;).arg(i));<br /> parentB-&gt;appendRow(itemB);<br /> parentB = itemB;<br /> list.append(itemB);<br /> }
<br /> QStandardItem *parentC = sourceModel-&gt;invisibleRootItem();<br /> for (int i = 0; i &lt; 3;i) {<br /> itemC = new QStandardItem(QString(&quot;C %0&amp;quot;).arg(i));<br /> parentC-&gt;appendRow(itemC);<br /> parentC = itemC;<br /> list.append(itemC);
<br /> }
<br /> proxyModel = new SortProxy(this);<br /> proxyModel-&gt;setSourceModel(sourceModel);<br /> setModel(proxyModel);<br /> expandAll();<br /> }<br /> QStandardItem *itemA;<br /> QStandardItem *itemA3;<br /> QStandardItem *itemA4;<br /> QStandardItem *itemNonA;<br /> QStandardItem *itemB;<br /> QStandardItem '''itemC;<br />};

<br />#include &quot;main.moc&amp;quot;
<br />int main(int argc, char'''*argv)<br />{<br /> QApplication app(argc, argv);<br /> QWidget widget;<br /> QPushButton *button = new QPushButton(&quot;Make only A1'' 'A' children visible&amp;quot;, &amp;widget);<br /> Tree *tree = new Tree(&amp;widget);<br /> QVBoxLayout *lay = new QVBoxLayout(&amp;widget);<br /> lay</s>&gt;addWidget(button);<br /> QObject::connect(button, SIGNAL (clicked()), proxyModel, SLOT (hideEverythingButA1AndChildren()));<br /> lay-&gt;addWidget(tree);<br /> widget.show();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br />