Re: Problem with sorting ColumnView

Andrew Potter via gtkmm-list <[email protected]> Tue, 23 Aug 2022 12:37:59 -0700
Newsgroups gmane.comp.gnome.gtkmm
Message-ID <CABgWb1LJHMo3iQniE1Zk4HX0sCHUZGsnU1Knv9a+fy+KNA8hqg@mail.gmail.com>
--===============8947207306224286330==
Content-Type: multipart/alternative; boundary="00000000000058f9c905e6edb481"

--00000000000058f9c905e6edb481
Content-Type: text/plain; charset="UTF-8"

The documentation at
https://docs.gtk.org/gtk4/method.ColumnView.get_sorter.html suggests you
need a SortListModel

On Tue, Aug 23, 2022 at 4:32 AM Jackson Campolattaro via gtkmm-list <
[email protected]> wrote:

> Hello,
>
> I'm trying to use the Gtk ColumnView to show a custom ListModel in sorted
> order. I noticed that when I set a simple sorter for a column and then sort
> by that column, the order isn't changed. The UI allows me to switch between
> ascending and descending, but doing so doesn't reverse the list.
>
> I've created a simplified example, which is supposed to show the file
> paths in the current directory in alphabetical order, but doesn't:
>
> #include <gtkmm.h>
>> #include <giomm.h>
>>
>> class MyWindow : public Gtk::Window {
>> private:
>>
>>     Glib::RefPtr<Gio::ListStore<Gio::File>> _listModel;
>>     Glib::RefPtr<Gtk::SingleSelection> _selectionModel;
>>
>>     Gtk::ColumnView _columnView;
>>
>> public:
>>     MyWindow() {
>>
>>         _listModel = Gio::ListStore<Gio::File>::create();
>>         _selectionModel = Gtk::SingleSelection::create(_listModel);
>>         _columnView.set_model(_selectionModel);
>>
>>         auto files =
>> Gio::File::create_for_path(".")->enumerate_children();
>>         Glib::RefPtr<Gio::FileInfo> fileInfo;
>>         while ((fileInfo = files->next_file())) {
>>
>> _listModel->append(Gio::File::create_for_path(fileInfo->get_name()));
>>         }
>>
>>         auto _factory = Gtk::SignalListItemFactory::create();
>>         _factory->signal_setup().connect([](auto listItem) {
>>
>> listItem->set_child(*Gtk::make_managed<Gtk::Label>("Placeholder!"));
>>         });
>>         _factory->signal_bind().connect([](auto listItem) {
>>             auto data =
>> std::dynamic_pointer_cast<Gio::File>(listItem->get_item());
>>             auto label = dynamic_cast<Gtk::Label
>> *>(listItem->get_child());
>>             if (data && label) {
>>                 label->set_text(data->get_path());
>>                 label->set_xalign(0);
>>             }
>>         });
>>
>>         auto column = Gtk::ColumnViewColumn::create("File Paths",
>> _factory);
>>         _columnView.append_column(column);
>>
>>         column->set_sorter(Gtk::StringSorter::create(
>>                 Gtk::ClosureExpression<Glib::ustring>::create([](auto
>> item) {
>>                     auto data =
>> std::dynamic_pointer_cast<Gio::File>(item);
>>                     return data->get_path();
>>                 })
>>         ));
>>         _columnView.sort_by_column(column, Gtk::SortType::ASCENDING);
>>
>>         set_child(_columnView);
>>     };
>> };
>>
>> int main(int argc, char *argv[]) {
>>     auto app = Gtk::Application::create("org.gtkmm.examples.base");
>>     return app->make_window_and_run<MyWindow>(argc, argv);
>> }
>>
>
> Perhaps this is a bug, but before I report it I wanted to make sure I'm
> not doing something wrong.
>
> Thanks,
>
> Jackson
> _______________________________________________
> gtkmm-list mailing list
> [email protected]
> https://mail.gnome.org/mailman/listinfo/gtkmm-list
>

--00000000000058f9c905e6edb481
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">The documentation at=C2=A0<a href=3D"https://docs.gtk.org/=
gtk4/method.ColumnView.get_sorter.html">https://docs.gtk.org/gtk4/method.Co=
lumnView.get_sorter.html</a> suggests you need a SortListModel=C2=A0</div><=
br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Tue,=
 Aug 23, 2022 at 4:32 AM Jackson Campolattaro via gtkmm-list &lt;<a href=3D=
"mailto:[email protected]">[email protected]</a>&gt; wrote:<br></div>=
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div>Hel=
lo,</div><div><br></div><div>I&#39;m trying to use the Gtk ColumnView to sh=
ow a custom ListModel in sorted order. I noticed that when I set a simple s=
orter for a column and then sort by that column, the order isn&#39;t change=
d. The UI allows me to switch between ascending and descending, but doing s=
o doesn&#39;t reverse the list.<br></div><div><br></div><div>I&#39;ve creat=
ed a simplified example, which is supposed to show the file paths in the cu=
rrent directory in alphabetical order, but doesn&#39;t:</div><div><br></div=
><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border=
-left:1px solid rgb(204,204,204);padding-left:1ex"><div><span style=3D"font=
-family:monospace">#include &lt;gtkmm.h&gt;<br>#include &lt;giomm.h&gt;<br>=
<br>class MyWindow : public Gtk::Window {<br>private:<br><br>=C2=A0 =C2=A0 =
Glib::RefPtr&lt;Gio::ListStore&lt;Gio::File&gt;&gt; _listModel;<br>=C2=A0 =
=C2=A0 Glib::RefPtr&lt;Gtk::SingleSelection&gt; _selectionModel;<br><br>=C2=
=A0 =C2=A0 Gtk::ColumnView _columnView;<br><br>public:<br>=C2=A0 =C2=A0 MyW=
indow() {<br><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 _listModel =3D Gio::ListStore&=
lt;Gio::File&gt;::create();<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 _selectionModel =
=3D Gtk::SingleSelection::create(_listModel);<br>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 _columnView.set_model(_selectionModel);<br><br>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 auto files =3D Gio::File::create_for_path(&quot;.&quot;)-&gt;enumerate_=
children();<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 Glib::RefPtr&lt;Gio::FileInfo&gt=
; fileInfo;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 while ((fileInfo =3D files-&gt;n=
ext_file())) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 _listModel-&gt;=
append(Gio::File::create_for_path(fileInfo-&gt;get_name()));<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 }<br><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 auto _factory =3D Gt=
k::SignalListItemFactory::create();<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 _factory=
-&gt;signal_setup().connect([](auto listItem) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 listItem-&gt;set_child(*Gtk::make_managed&lt;Gtk::Label&g=
t;(&quot;Placeholder!&quot;));<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 });<br>=C2=A0=
 =C2=A0 =C2=A0 =C2=A0 _factory-&gt;signal_bind().connect([](auto listItem) =
{<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto data =3D std::dynamic_p=
ointer_cast&lt;Gio::File&gt;(listItem-&gt;get_item());<br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 auto label =3D dynamic_cast&lt;Gtk::Label *&gt;(li=
stItem-&gt;get_child());<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (d=
ata &amp;&amp; label) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 label-&gt;set_text(data-&gt;get_path());<br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 label-&gt;set_xalign(0);<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 });<br><br=
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 auto column =3D Gtk::ColumnViewColumn::create(=
&quot;File Paths&quot;, _factory);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 _columnVi=
ew.append_column(column);<br><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 column-&gt;set=
_sorter(Gtk::StringSorter::create(<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 Gtk::ClosureExpression&lt;Glib::ustring&gt;::create([]=
(auto item) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 auto data =3D std::dynamic_pointer_cast&lt;Gio::File&gt;(item=
);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 return data-&gt;get_path();<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 })<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 ));<br>=C2=A0 =C2=A0 =C2=A0=
 =C2=A0 _columnView.sort_by_column(column, Gtk::SortType::ASCENDING);<br><b=
r>=C2=A0 =C2=A0 =C2=A0 =C2=A0 set_child(_columnView);<br>=C2=A0 =C2=A0 };<b=
r>};<br><br>int main(int argc, char *argv[]) {<br>=C2=A0 =C2=A0 auto app =
=3D Gtk::Application::create(&quot;org.gtkmm.examples.base&quot;);<br>=C2=
=A0 =C2=A0 return app-&gt;make_window_and_run&lt;MyWindow&gt;(argc, argv);<=
br>}</span></div></blockquote><div><br></div><div>Perhaps this is a bug, bu=
t before I report it I wanted to make sure I&#39;m not doing something wron=
g.</div><div><br></div><div>Thanks,</div><div><br></div><div>Jackson</div><=
/div>
_______________________________________________<br>
gtkmm-list mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">gtkmm-list@gnome.=
org</a><br>
<a href=3D"https://mail.gnome.org/mailman/listinfo/gtkmm-list" rel=3D"noref=
errer" target=3D"_blank">https://mail.gnome.org/mailman/listinfo/gtkmm-list=
</a><br>
</blockquote></div>

--00000000000058f9c905e6edb481--

--===============8947207306224286330==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
gtkmm-list mailing list
[email protected]
https://mail.gnome.org/mailman/listinfo/gtkmm-list

--===============8947207306224286330==--