1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
hljs.configure({languages: ['cpp']});
hljs.highlightAll();
</script><title>Sorting</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-treeview.html" title="Chapter 11. The TreeView widget">
<link rel="prev" href="sec-treeview-selection.html" title="The Selection">
<link rel="next" href="sec-treeview-draganddrop.html" title="Drag and Drop">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Sorting</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-treeview-selection.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 11. The TreeView widget</th>
<td width="20%" align="right"> <a accesskey="n" href="sec-treeview-draganddrop.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-treeview-sort"></a>Sorting</h2></div></div></div>
<p>
The standard tree models (<code class="classname">TreeStore</code> and <code class="classname">ListStore</code>) derive from <code class="classname">TreeSortable</code>, so they offer sorting functionality. For instance, call <code class="methodname">set_sort_column()</code>, to sort the model by the specified column. Or supply a callback function to <code class="methodname">set_sort_func()</code> to implement a more complicated sorting algorithm.
</p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1TreeSortable.html" target="_top">TreeSortable Reference</a></p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="treeview-sort-headers"></a>Sorting by clicking on columns</h3></div></div></div>
<p>
So that a user can click on a <code class="classname">TreeView</code>'s column header to sort the <code class="classname">TreeView</code>'s contents, call <code class="methodname">Gtk::TreeView::Column::set_sort_column()</code>, supplying the model column on which model should be sorted when the header is clicked. For instance:
</p>
<pre class="programlisting"><code class="code">auto pColumn = treeview.get_column(0);
if(pColumn)
pColumn->set_sort_column(m_columns.m_col_id);</code></pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="treeview-sort-independent-views"></a>Independently sorted views of the same model</h3></div></div></div>
<p>
The <code class="classname">TreeView</code> already allows you to show the same <code class="classname">TreeModel</code>
in two <code class="classname">TreeView</code> widgets. If you need one of these TreeViews to sort the model
differently than the other then you should use a <code class="classname">TreeModelSort</code> instead of just,
for instance, <code class="methodname">Gtk::TreeViewColumn::set_sort_column()</code>.
<code class="classname">TreeModelSort</code> is a model that contains another model, presenting a sorted version
of that model. For instance, you might add a sorted version of a model to a <code class="classname">TreeView</code> like so:
</p>
<pre class="programlisting"><code class="code">auto sorted_model = Gtk::TreeModelSort::create(model);
sorted_model->set_sort_column(columns.m_col_name, Gtk::SortType::ASCENDING);
treeview.set_model(sorted_model);</code></pre>
<p>Note, however, that the TreeView will provide iterators to the sorted model. You must convert them to iterators to the underlying child model in order to perform actions on that model. For instance:
</p>
<pre class="programlisting"><code class="code">void ExampleWindow::on_button_delete()
{
auto refTreeSelection = m_treeview.get_selection();
if(refTreeSelection)
{
auto sorted_iter = m_refTreeSelection->get_selected();
if(sorted_iter)
{
auto iter = m_refModelSort->convert_iter_to_child_iter(sorted_iter);
m_refModel->erase(iter);
}
}
}</code></pre>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1TreeModelSort.html" target="_top">TreeModelSort Reference</a></p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-treeview-selection.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-treeview.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="sec-treeview-draganddrop.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">The Selection </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Drag and Drop</td>
</tr>
</table>
</div>
</body>
</html>
|