Problem with sorting ColumnView
Jackson Campolattaro via gtkmm-list <[email protected]> Tue, 23 Aug 2022 13:32:24 +0200
| Newsgroups | gmane.comp.gnome.gtkmm |
|---|---|
| Message-ID | <CAK-p+x8OsMTvTkzK94XVOi1c2Fe_mAx--qtVBdStp55fONUG6w@mail.gmail.com> |
--===============3136905080133877254==
Content-Type: multipart/alternative; boundary="000000000000c74bde05e6e6ebbf"
--000000000000c74bde05e6e6ebbf
Content-Type: text/plain; charset="UTF-8"
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
--000000000000c74bde05e6e6ebbf
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>Hello,</div><div><br></div><div>I'm trying to use=
the Gtk ColumnView to show a custom ListModel in sorted order. I noticed t=
hat when I set a simple sorter for a column and then sort by that column, t=
he order isn't changed. The UI allows me to switch between ascending an=
d descending, but doing so doesn't reverse the list.<br></div><div><br>=
</div><div>I've created a simplified example, which is supposed to show=
the file paths in the current 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 <gtkmm.h><br>#in=
clude <giomm.h><br><br>class MyWindow : public Gtk::Window {<br>priva=
te:<br><br>=C2=A0 =C2=A0 Glib::RefPtr<Gio::ListStore<Gio::File>>=
; _listModel;<br>=C2=A0 =C2=A0 Glib::RefPtr<Gtk::SingleSelection> _se=
lectionModel;<br><br>=C2=A0 =C2=A0 Gtk::ColumnView _columnView;<br><br>publ=
ic:<br>=C2=A0 =C2=A0 MyWindow() {<br><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 _listM=
odel =3D Gio::ListStore<Gio::File>::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("=
;.")->enumerate_children();<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 Glib::Re=
fPtr<Gio::FileInfo> fileInfo;<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 while ((=
fileInfo =3D files->next_file())) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 _listModel->append(Gio::File::create_for_path(fileInfo->ge=
t_name()));<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 }<br><br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 auto _factory =3D Gtk::SignalListItemFactory::create();<br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 _factory->signal_setup().connect([](auto listItem) =
{<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 listItem->set_child(*Gtk:=
:make_managed<Gtk::Label>("Placeholder!"));<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 });<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 _factory->signal_bi=
nd().connect([](auto listItem) {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 auto data =3D std::dynamic_pointer_cast<Gio::File>(listItem->g=
et_item());<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto label =3D dyn=
amic_cast<Gtk::Label *>(listItem->get_child());<br>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (data && label) {<br>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 label->set_text(data->get_p=
ath());<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 label-&g=
t;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("File Paths", _factory);<br>=C2=A0=
=C2=A0 =C2=A0 =C2=A0 _columnView.append_column(column);<br><br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 column->set_sorter(Gtk::StringSorter::create(<br>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Gtk::ClosureExpression=
<Glib::ustring>::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_po=
inter_cast<Gio::File>(item);<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return data->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(colum=
n, Gtk::SortType::ASCENDING);<br><br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 set_child(=
_columnView);<br>=C2=A0 =C2=A0 };<br>};<br><br>int main(int argc, char *arg=
v[]) {<br>=C2=A0 =C2=A0 auto app =3D Gtk::Application::create("org.gtk=
mm.examples.base");<br>=C2=A0 =C2=A0 return app->make_window_and_ru=
n<MyWindow>(argc, argv);<br>}</span></div></blockquote><div><br></div=
><div>Perhaps this is a bug, but before I report it I wanted to make sure I=
'm not doing something wrong.</div><div><br></div><div>Thanks,</div><di=
v><br></div><div>Jackson</div></div>
--000000000000c74bde05e6e6ebbf--
--===============3136905080133877254==
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
--===============3136905080133877254==--