wxDataViewListCtrl sorting - appearance is correct but returned row data is unsorted

50 views
Skip to first unread message

Andreas Falkenhahn

unread,
May 9, 2021, 3:15:51 PMMay 9
to wx-u...@googlegroups.com
When I create a sortable column for my wxDataViewListCtrl and click on the header, it will correctly show the data either in ascending and descending order in the listview column.

The actual data, however, will never be returned in a sorted order. It will always be returned in the order it was inserted. e.g. doing

for(int k = 0; k < dv->GetItemCount(); k++) {
wxString s = dv->GetTextValue(k, 0);
}

will always returns the data in the order it was inserted, no matter if the sort mode is currently set to ascending or descending.

Is this supposed to behave like that? How can I find out the contents of the selected row then? Whenever sorting is active, the row content shown in the widget and the row content returned by GetTextValue() mismatch. Quite confusing. Am I doing anything wrong here or what's going on?

Tested on MSW.

--
Best regards,
Andreas Falkenhahn mailto:and...@falkenhahn.com

Andreas Falkenhahn

unread,
May 10, 2021, 9:35:25 AMMay 10
to Andreas Falkenhahn
I've checked out the source code and now I'm more confused.

Apparently, rows are always unsorted in wxDataViewListStore. For the generic dataview control, the actual sorting happens in a class named wxDataViewTreeNode. But this class seems to be completely inaccessible from the outside! How can that be?

Suppose I add the rows C,B,A into a wxDataViewListCtrl. Then I click on the header to sort them in ascending order. The order is A,B,C now. Now I want to find out which item is at row 0. This should return A because the items are now sorted. However, I don't see how I can get this information. All methods operating on the store will still give me item C for row 0 because the list store has the rows in unsorted order and I don't see any way to access the actual sorted data in wxDataViewTreeNode.

But it must be possible to get this! Otherwise the sort feature would be completely useless. But how to do it?

It's really annoying that such basic functionality is so difficult! I can't possibly be the only one who has ever tried to sort a wxDataViewListCtrl and wondered how to get back the sorted data from the control...

d0

unread,
May 10, 2021, 9:46:25 AMMay 10
to wx-u...@googlegroups.com
FWIW I only bother with toolkit-provided sorting for the "small data" usecase, and in that case I'm not using the virtual/model-based widgets.

In all other cases it was always better to tell the backend (whatever it is) to provide the data sorted. In wx you can do that with wxEVT_DATAVIEW_COLUMN_SORTED. But I think this would be incompatible with your approach using wxDataViewListCtrl. It's an odd class.

Cheers, Marian

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+u...@googlegroups.com
or visit http://groups.google.com/group/wx-users
---
You received this message because you are subscribed to the Google Groups "wx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wx-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wx-users/928582058.20210510153535%40falkenhahn.com.

Vadim Zeitlin

unread,
May 10, 2021, 9:47:12 AMMay 10
to wx-u...@googlegroups.com
On Mon, 10 May 2021 15:35:35 +0200 Andreas Falkenhahn wrote:

AF> It's really annoying that such basic functionality is so difficult! I
AF> can't possibly be the only one who has ever tried to sort a
AF> wxDataViewListCtrl and wondered how to get back the sorted data from
AF> the control...

Sorry, I just really have no time/energy to deal with wxDataViewListCtrl
right now. As I already said, IME you will save a lot of time by not using
it at all. The basic wxDataViewCtrl architecture is mostly sound, although
still sometimes surprising. I don't think anybody has ever been happy with
using wxDataViewListCtrl and I have no idea why do you insist on dealing
with it.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

mailing lists

unread,
May 10, 2021, 10:02:56 AMMay 10
to wx-u...@googlegroups.com
Hi Andreas,

I would not suggest to use wxDataViewListCtrl, either. Actually, wxDataViewListCtrl was a rather quick hack on top of wxDataViewCtrl. As Vadim said, just use wxDataViewCtrl and you are much better off.

Regards,
Hardie

Andreas Falkenhahn

unread,
May 10, 2021, 10:06:14 AMMay 10
to Vadim Zeitlin
On 10.05.2021 at 15:47 Vadim Zeitlin wrote:

> Sorry, I just really have no time/energy to deal with wxDataViewListCtrl
> right now. As I already said, IME you will save a lot of time by not using
> it at all. The basic wxDataViewCtrl architecture is mostly sound, although
> still sometimes surprising. I don't think anybody has ever been happy with
> using wxDataViewListCtrl and I have no idea why do you insist on dealing
> with it.

It's not necessarily a wxDataViewListCtrl thing. As with my issue concerning
column insertion last week, it turned out that it had nothing to do with
wxDataViewListCtrl but turned out to be a general wxWidgets bug. So all the
blame heaped on wxDataViewListCtrl turned out to be merely FUD...

And even if I ditched wxDataViewListCtrl I'd probably have to re-create something
very similar to it because it has exactly the APIs I need. And I have used it
for lots of years on MSW/GTK/Mac without problems so I don't understand
why I should drop something that works nicely, especially concerning that I haven't
heard any substantial arguments as to wxDataViewListCtrl should be avoided.

So how would you deal with the problem when using wxDataViewCtrl directly?
I don't understand this at all. The generic dataview control still does
the sorting in its wxDataViewTreeNode class which I don't know how to access.
So it doesn't matter if I use wxDataViewCtrl or wxDataViewListCtrl. The
sorting still happens in some part of the widget that I don't have access
to AFAICS. So I think this is a general problem here or is that wrong?
How would you try to get back the sorted data from the wxDataViewCtrl
once the user has clicked the header to sort the data?

Vadim Zeitlin

unread,
May 10, 2021, 10:22:21 AMMay 10
to wx-u...@googlegroups.com
On Mon, 10 May 2021 16:06:23 +0200 Andreas Falkenhahn wrote:

AF> So how would you deal with the problem when using wxDataViewCtrl directly?

Please formulate the problem when dealing with wxDataViewCtrl directly
because I don't understand what it is, sorry. All I can say is that I've
used wxDataViewCtrl with quite a few different models in my code and I
could implement sorting for it without any particular problems AFAIR.

And the dataview sample shows how to sort its own (admittedly, also rather
weird and not very realistic) model. Can you explain what is different and
doesn't work in your case? As always, a minimal patch to the sample
allowing to reproduce the problem you're having would be very welcome.

Of course, if you prefer to keep debugging wxDataViewListCtrl, you should
also be able to make it work too, eventually. I was just trying to help
because I believe it's not the most productive use of your time, but,
first, I could be wrong, and, second, it's up to you to decide.

Andreas Falkenhahn

unread,
May 10, 2021, 1:22:28 PMMay 10
to Vadim Zeitlin
On 10.05.2021 at 16:22 Vadim Zeitlin wrote:

> Please formulate the problem when dealing with wxDataViewCtrl directly
> because I don't understand what it is, sorry. All I can say is that I've
> used wxDataViewCtrl with quite a few different models in my code and I
> could implement sorting for it without any particular problems AFAIR.

Here you go. I'm trying to do a really simple thing: I just want to get
the contents of a row by index. Start the program attached, click the
button, the message box will show "C". This is correct because "C" is
in row number 0. Now click the header to reverse the sort order. Now
click the button again. It will still report "C" although "A" is in row
number 0 now. How can I make it to return "A" in that case? I just want
to be able to get a sorted row's contents by index... it's a really
simple thing...

> Of course, if you prefer to keep debugging wxDataViewListCtrl, you should
> also be able to make it work too, eventually. I was just trying to help
> because I believe it's not the most productive use of your time, but,
> first, I could be wrong, and, second, it's up to you to decide.

I really need to get this to work with wxDataViewListCtrl because
I have written lots of code for it which works perfectly fine so I
don't really want to ditch wxDataViewListCtrl. As soon as you tell me
how to do it with wxDataViewCtrl I can probably easily adapt your
solution to work with wxDataViewListCtrl as well... thanks!
test.cpp

d0

unread,
May 10, 2021, 2:02:34 PMMay 10
to wx-u...@googlegroups.com
>  Here you go. I'm trying to do a really simple thing: I just want to get
the contents of a row by index. Start the program attached, click the
button, the message box will show "C". This is correct because "C" is
in row number 0. Now click the header to reverse the sort order. Now
click the button again. It will still report "C" although "A" is in row
number 0 now. How can I make it to return "A" in that case? I just want
to be able to get a sorted row's contents by index... it's a really
simple thing...

It seems to me that the automatic sorting in dataview is only meant to sort the data as displayed by the view, and isn't meant to sort the underlying model.
The rows and columns you are working with are model rows and columns; the dataview doesn't know you are indexing by row. Dataview only works on item + column number (as mapped by wxDataViewColumn), I'd wager using model->GetValue(..., view->GetTopItem(), <column index>) would return the answers you seek.

Cheers, Marian

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+u...@googlegroups.com
or visit http://groups.google.com/group/wx-users
---
You received this message because you are subscribed to the Google Groups "wx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wx-users+u...@googlegroups.com.

Andreas Falkenhahn

unread,
May 10, 2021, 2:37:51 PMMay 10
to d0
On 10.05.2021 at 20:02 d0 wrote:

> It seems to me that the automatic sorting in dataview is only meant
> to sort the data as displayed by the view, and isn't meant to sort the underlying model.

Yes, that's what I've already found out and mentioned earlier. Sorting
definitely doesn't happen in the model. Still, there must be some
way to get the rows as they are displayed by the view. That's what
I'm after...

> The rows and columns you are working with are model rows and
> columns; the dataview doesn't know you are indexing by row. Dataview
> only works on item + column number (as mapped by wxDataViewColumn),
> I'd wager using model->GetValue(..., view->GetTopItem(), <column
> index>) would return the answers you seek.

GetTopItem() doesn't really help. Of course, I want to be able to get
the contents for *all* rows, not just for the top one. That was just
an example for simplicity's sake...

d0

unread,
May 10, 2021, 3:01:52 PMMay 10
to wx-u...@googlegroups.com
For dataview to work this isn't necessary, so I don't quite follow on the "there must be a way to get [items in display order]" part. These built-in amenities like sorting only work for simple cases - “If it don't fits, I still sits.” (-T.A. Swift) only works sometimes. Use your own model and implement sorting there. Problem solved.

Cheers, Marian

Andreas Falkenhahn

unread,
May 10, 2021, 3:20:25 PMMay 10
to d0
I still refuse to believe that the only way to get this to work is to implement sorting on my own. Let's wait what Vadim has to say about it :)
> Cheers, Marian--
> Please read http://www.wxwidgets.org/support/mlhowto.htmbefore posting.
>
> To unsubscribe, send email to wx-users+u...@googlegroups.com
> or visit http://groups.google.com/group/wx-users
> ---
> You received this message because you are subscribed to the Google Groups "wx-users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to wx-users+u...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/wx-users/CAEi-PsvOGRcWvNp0hjJ9gSsk71ftb7KNiKZjRxDrWGW7e9o6hQ%40mail.gmail.com.

Vadim Zeitlin

unread,
May 10, 2021, 5:00:55 PMMay 10
to wx-u...@googlegroups.com
On Mon, 10 May 2021 19:22:37 +0200 Andreas Falkenhahn wrote:

AF> On 10.05.2021 at 16:22 Vadim Zeitlin wrote:
AF>
AF> > Please formulate the problem when dealing with wxDataViewCtrl directly
AF> > because I don't understand what it is, sorry. All I can say is that I've
AF> > used wxDataViewCtrl with quite a few different models in my code and I
AF> > could implement sorting for it without any particular problems AFAIR.
AF>
AF> Here you go. I'm trying to do a really simple thing: I just want to get
AF> the contents of a row by index. Start the program attached, click the
AF> button, the message box will show "C". This is correct because "C" is
AF> in row number 0. Now click the header to reverse the sort order. Now
AF> click the button again. It will still report "C" although "A" is in row
AF> number 0 now. How can I make it to return "A" in that case? I just want
AF> to be able to get a sorted row's contents by index... it's a really
AF> simple thing...

Sorry, perhaps it's simple, but it's based on a fundamental
misunderstanding, i.e. you expect the model data to be sorted by the view
and wxDataViewCtrl simply doesn't work at all like this.

IOW, you're using model GetValueByRow() method with hard-coded row and
expect its value to somehow change, but of course the model row value
doesn't change depending on the sort order, how could it possible do this?
Just think about using the same model from 2 different controls, each with
their own sort order. Should the model be in a quantum superposition of 2
states? Of course not, the model doesn't change, the sort order only exists
at the view level.

But then, your UI code also works at the view level, normally. So why do
you use the row index, rather than the item in the first place? If you use
the current item, rather than hard-coded 0, in your callback, you can use
GetValue(item) to get its value and it will reflect the current sort order.
I.e. try doing this

m->GetValue(v, dv->GetSelection(), 0);

in your code instead of

m->GetValueByRow(v, 0, 0);


You could also get the sort column and sort order from the control and use
them to get the data from the model if you want. This can be useful when
you can optimize data access using this. But you just can't possible expect
what you're trying to do to work at all.

Andreas Falkenhahn

unread,
May 10, 2021, 5:32:06 PMMay 10
to Vadim Zeitlin
On 10.05.2021 at 23:00 Vadim Zeitlin wrote:

> IOW, you're using model GetValueByRow() method with hard-coded row and
> expect its value to somehow change, but of course the model row value
> doesn't change depending on the sort order, how could it possible do this?
> Just think about using the same model from 2 different controls, each with
> their own sort order. Should the model be in a quantum superposition of 2
> states? Of course not, the model doesn't change, the sort order only exists
> at the view level.

But isn't that some sort of design flaw in wxDataViewCtrl? I mean, let's
suppose I just want to export the data in *exactly* the same order as it
appears in the wxDataViewCtrl. This is not an uncommon task at all but
as it looks, I just can't do this with wxDataViewCtrl unless I do the
sorting on my own in the model and somehow intercept the automatic sorting
that happens when the user clicks on the header.

I fully understand the distinction between model and view data and I see
nothing wrong with such a design. But what I don't understand is that wxDataViewCtrl
apparently doesn't allow me to *access* the view data. The view data seems
to be completely inaccessible to programmers, maybe except for getting
the top item or the currently selected item but apparently not the order
of all items. IMHO this makes the sorting feature completely useless
when dealing with lists because obviously you often want to export the
data in the way it appears in the control.... but that's just not possible.

I think model and view data should've been linked in some way so that I
can easily tell which row in the model goes with which row in the view
or something like that.

Vadim Zeitlin

unread,
May 10, 2021, 5:41:58 PMMay 10
to wx-u...@googlegroups.com
On Mon, 10 May 2021 23:32:15 +0200 Andreas Falkenhahn wrote:

AF> But isn't that some sort of design flaw in wxDataViewCtrl?

No, it's a pretty standard application of the usual MVC design.

AF> I mean, let's suppose I just want to export the data in exactly the
AF> same order as it appears in the wxDataViewCtrl.

You just need to get the items in their display order. wxDVC should
provide methods for getting them, similar to wxListCtrl::GetNextItem(), and
it's indeed a missing feature in it, but this is not at all a design
problem.

AF> I fully understand the distinction between model and view data and I see
AF> nothing wrong with such a design. But what I don't understand is that wxDataViewCtrl
AF> apparently doesn't allow me to access the view data.

But there is no data in the view, that's the whole point...

Again, all you need is just a way to iterate over the items of wxDVC. This
is a perfectly reasonable thing to do and we certainly can implement it in
the generic version. I'm less sure about the GTK one, while using
gtk_tree_view_get_path_at_pos() would work, it doesn't seem like the
simplest or most efficient way to do it, there must be a better way to do
it...

Andreas Falkenhahn

unread,
May 12, 2021, 7:38:35 AMMay 12
to Vadim Zeitlin
On 10.05.2021 at 23:41 Vadim Zeitlin wrote:

> You just need to get the items in their display order. wxDVC should
> provide methods for getting them, similar to wxListCtrl::GetNextItem(), and
> it's indeed a missing feature in it, but this is not at all a design
> problem.

Well, that's what I meant by "design problem" then. I consider missing
core functionality like being able to get items in display order a
design problem in the API :)

> Again, all you need is just a way to iterate over the items of wxDVC. This
> is a perfectly reasonable thing to do and we certainly can implement it in
> the generic version. I'm less sure about the GTK one, while using
> gtk_tree_view_get_path_at_pos() would work, it doesn't seem like the
> simplest or most efficient way to do it, there must be a better way to do
> it...

Well, I'm just going with d0's solution now because it seems to be the
easiest way to work around this limitation of not being able to get the
items in display order because everything else seems to require in-depth
work on the generic, the GTK, and the Cocoa version of wxDataViewCtrl
which looks like a huge endeavour.

So I'm just intercepting the header click now and sort the data in my
model. I've tested this with the little example I sent to the list on
Monday and it seems to work. On wxEVT_DATAVIEW_COLUMN_HEADER_CLICK I
simply do the following now:

static void evtcallback2(wxDataViewEvent &event)
{
ascending = !ascending;

dv->GetColumn(0)->ToggleSortOrder();
((MyIndexListModel *) dv->GetModel())->SortStuff(ascending);

// HOW TO TELL THE CONTROL TO REFRESH??!?!
for(int k = 0; k < 3; k++) ((wxDataViewIndexListModel *) dv->GetModel())->RowValueChanged(k, 0);
}

But one thing that remains to be solved is how can I notify the
wxDataViewCtrl to refresh after having sorted the data in the model?

Do I really have to call wxDataViewIndexListModel::RowValueChanged()
for every row and every column? This looks like overkill to me,
especially considering that each call to RowValueChanged() will
also dispatch a wxEVT_DATAVIEW_ITEM_VALUE_CHANGED event.

Is there no better way to simply tell the wxDataViewCtrl to
refresh everything?

d0

unread,
May 12, 2021, 9:05:47 AMMay 12
to wx-u...@googlegroups.com
>  Is there no better way to simply tell the wxDataViewCtrl to
refresh everything?

Use model->Reset(row_count) (works on MSW+GTK)

Cheers, Marian

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.


To unsubscribe, send email to wx-users+u...@googlegroups.com
or visit http://groups.google.com/group/wx-users
---
You received this message because you are subscribed to the Google Groups "wx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wx-users+u...@googlegroups.com.

Vadim Zeitlin

unread,
May 12, 2021, 11:07:57 AMMay 12
to wx-u...@googlegroups.com
On Wed, 12 May 2021 13:38:47 +0200 Andreas Falkenhahn wrote:

AF> On 10.05.2021 at 23:41 Vadim Zeitlin wrote:
AF>
AF> > You just need to get the items in their display order. wxDVC should
AF> > provide methods for getting them, similar to wxListCtrl::GetNextItem(), and
AF> > it's indeed a missing feature in it, but this is not at all a design
AF> > problem.
AF>
AF> Well, that's what I meant by "design problem" then. I consider missing
AF> core functionality like being able to get items in display order a
AF> design problem in the API :)

In case you're being serious in spite of the smiley, I'd like to make it
clear that it's just a missing feature, which is very different from a
design problem.

Also, I still believe your view might be biased by the fact that you're
using a very simple model and so you don't implement your own Compare().
Typically you do, i.e. you already have the code for sorting the items
anyhow and it's pretty trivial to output them in the sorted order if you
really need to do it.

AF> So I'm just intercepting the header click now and sort the data in my
AF> model. I've tested this with the little example I sent to the list on
AF> Monday and it seems to work. On wxEVT_DATAVIEW_COLUMN_HEADER_CLICK I
AF> simply do the following now:
AF>
AF> static void evtcallback2(wxDataViewEvent &event)
AF> {
AF> ascending = !ascending;
AF>
AF> dv->GetColumn(0)->ToggleSortOrder();
AF> ((MyIndexListModel *) dv->GetModel())->SortStuff(ascending);
AF>
AF> // HOW TO TELL THE CONTROL TO REFRESH??!?!
AF> for(int k = 0; k < 3; k++) ((wxDataViewIndexListModel *) dv->GetModel())->RowValueChanged(k, 0);
AF> }

You really shouldn't do that, this is not how things are supposed to work.
But I think I've said as much as I could on this topic, if you prefer to
make wxDVC behave as a non-MVC control, it's, again, your choice. I don't
think it will end well, but maybe I'm wrong.

AF> But one thing that remains to be solved is how can I notify the
AF> wxDataViewCtrl to refresh after having sorted the data in the model?

This has already been answered and isn't really a problem. The problem is
that you're modifying the model in response to a view event which is just
not how things are supposed to be done.

Andreas Falkenhahn

unread,
May 12, 2021, 11:51:39 AMMay 12
to Vadim Zeitlin
On 12.05.2021 at 17:07 Vadim Zeitlin wrote:

> You really shouldn't do that, this is not how things are supposed to work.
> But I think I've said as much as I could on this topic, if you prefer to
> make wxDVC behave as a non-MVC control, it's, again, your choice. I don't
> think it will end well, but maybe I'm wrong.

But what is the alternative? AFAIU the only alternative you suggested was
to implement the missing functionality in wxDVC so that it's possible to get
the items in their display order. And you weren't even sure if that was
possible for wxGTK at all... so that's not really a promising prospect...

> This has already been answered and isn't really a problem. The problem is
> that you're modifying the model in response to a view event which is just
> not how things are supposed to be done.

Yet AFAIU this is exactly what d0 suggested and nobody intervened and said
"No, don't do it like that!". It works with the generic control on MSW. Haven't
tested macOS and GTK yet, though.

Vadim Zeitlin

unread,
May 12, 2021, 11:55:34 AMMay 12
to wx-u...@googlegroups.com
On Wed, 12 May 2021 17:51:53 +0200 Andreas Falkenhahn wrote:

AF> On 12.05.2021 at 17:07 Vadim Zeitlin wrote:
AF>
AF> > You really shouldn't do that, this is not how things are supposed to work.
AF> > But I think I've said as much as I could on this topic, if you prefer to
AF> > make wxDVC behave as a non-MVC control, it's, again, your choice. I don't
AF> > think it will end well, but maybe I'm wrong.
AF>
AF> But what is the alternative?

Just dump all the items in the current sort order if this is what you
need. You can either reuse the model Compare() to do it, or implement your
own Compare() and reuse the code in it in your dump function.

AF> AFAIU the only alternative you suggested was to implement the missing
AF> functionality in wxDVC so that it's possible to get the items in their
AF> display order. And you weren't even sure if that was possible for wxGTK
AF> at all... so that's not really a promising prospect...

I said that it was possible to do it at least using the function I gave,
but might be not very efficient. This is a bit different.

AF> > This has already been answered and isn't really a problem. The problem is
AF> > that you're modifying the model in response to a view event which is just
AF> > not how things are supposed to be done.
AF>
AF> Yet AFAIU this is exactly what d0 suggested and nobody intervened and said
AF> "No, don't do it like that!". It works with the generic control on MSW. Haven't
AF> tested macOS and GTK yet, though.

You can do it like this if the goal is to refresh the control, i.e. d0's
answer was correct for the question you've asked. I just don't think you
should be changing the entire contents of your model (because this is what
Reset() does) when you've only sorted it, because wxDVC has special support
for sorting. E.g. I can easily imagine that calling Reset() could be much
less efficient.

Andreas Falkenhahn

unread,
May 12, 2021, 12:16:39 PMMay 12
to Vadim Zeitlin
On 12.05.2021 at 17:55 Vadim Zeitlin wrote:

> Just dump all the items in the current sort order if this is what you
> need. You can either reuse the model Compare() to do it, or implement your
> own Compare() and reuse the code in it in your dump function.

But wouldn't that be overhead galore? Suppose I wanted to know the item
at row N in the current sort order. To get that, I'd have to dump all
items and sort them to find out what's at row N.

Maybe I could just keep a separate copy of all rows in the model and just
sort that copy on every wxDATAVIEW_COLUMN_HEADER_CLICK without touching the
main row store. Then I wouldn't have to interfere with wxDVC's inhouse sort
handler and wouldn't have to modify the model in wxDATAVIEW_COLUMN_HEADER_CLICK
but just the copy of all rows kept separately.

But I'm still not sure if this would work because I also need things like
"delete row N" where N is a row in the current sort order - not in the
model's order. For this I'd need a way to do the same thing in reverse,
i.e. find out the corresponding model row for a given display row. It's
complicated :(

> I said that it was possible to do it at least using the function I gave,
> but might be not very efficient. This is a bit different.

Oops, sorry, that's right of course :)

Vadim Zeitlin

unread,
May 12, 2021, 2:28:52 PMMay 12
to wx-u...@googlegroups.com
On Wed, 12 May 2021 18:16:51 +0200 Andreas Falkenhahn wrote:

AF> On 12.05.2021 at 17:55 Vadim Zeitlin wrote:
AF>
AF> > Just dump all the items in the current sort order if this is what you
AF> > need. You can either reuse the model Compare() to do it, or implement your
AF> > own Compare() and reuse the code in it in your dump function.
AF>
AF> But wouldn't that be overhead galore? Suppose I wanted to know the item
AF> at row N in the current sort order.

We can suppose anything we want, but I'd rather suppose you can avoid
needing it in the first place. Although I can imagine wanting to dump all
items in the sort order, I just don't see why would you want to get one row
at the given index (which is, of course, not even an index in the first
place, but a sequence of indices, generally speaking, as the items in this
control form a tree and not just a flat list).

AF> But I'm still not sure if this would work because I also need things like
AF> "delete row N" where N is a row in the current sort order - not in the
AF> model's order.

Where does this "N" come from? Typically users select the item to operate
on by clicking on it, or otherwise selecting it. It seems very strange to
specify the items by their position rather than either by their item or
screen coordinates. Are you sure there is no way to do what you want in a
much simpler way?


Anyhow, to keep this discussion at least slightly productive, if you
really need it, you absolutely need a function returning a wxDataViewItem
for the corresponding index. You could implement it in your own code, but
doing it in wx itself would make more sense. After looking at GTK docs
again, I think it should be possible to do it there by using GtkTreePath,
which is just such a collection of indices that you want (and so just a
single index for the flat tree). IIUC you should be able to use
gtk_tree_path_new_from_indicesv(&index, 1) to create the path you need and
once you have the path you can use the existing GTKPathToItem(), so it
actually looks pretty simple.

You can also use gtk_tree_view_get_visible_range() and then
gtk_tree_path_next() to iterate over all the currently visible paths, even
in the general case (in your case it would be enough to just increment the
index, of course).


AF> For this I'd need a way to do the same thing in reverse, i.e. find out
AF> the corresponding model row for a given display row. It's complicated

Yes, not doing it would definitely be simpler...

Sergey Prokopenko

unread,
Sep 19, 2021, 10:06:14 AMSep 19
to wx-users

It would be nice to implement  wxDataViewCtrl::GetNextItem() similar to 
wxListCtrl::GetNextItem() as it was discussed earlier here:

>You just need to get the items in their display order. wxDVC should

>provide methods for getting them, similar to wxListCtrl::GetNextItem(), and
>it's indeed a missing feature in it, but this is not at all a design
>problem.

Sometimes, it is needed to select/get next item in wxDataViewCtrl programmatically (for example, by user click on button in different control to move to editing next item shown in wxDataViewCtrl). And now, the only way to find next item shown in wxDataViewCtrl is to reimplement in your own code the same sorting as currently active in  wxDataViewCtrl.

Thank you.
Sergey.
среда, 12 мая 2021 г. в 21:28:52 UTC+3, Vadim Zeitlin:

Vadim Zeitlin

unread,
Sep 19, 2021, 10:09:26 AMSep 19
to wx-u...@googlegroups.com
On Sun, 19 Sep 2021 07:04:51 -0700 (PDT) Sergey Prokopenko wrote:

SP> It would be nice to implement wxDataViewCtrl::GetNextItem() similar to
SP> wxListCtrl::GetNextItem() as it was discussed earlier here:
SP>
SP> >You just need to get the items in their display order. wxDVC should
SP> >provide methods for getting them, similar to wxListCtrl::GetNextItem(), and
SP> >it's indeed a missing feature in it, but this is not at all a design
SP> >problem.
SP>
SP> Sometimes, it is needed to select/get next item in wxDataViewCtrl
SP> programmatically (for example, by user click on button in different control
SP> to move to editing next item shown in wxDataViewCtrl). And now, the only
SP> way to find next item shown in wxDataViewCtrl is to reimplement in your own
SP> code the same sorting as currently active in wxDataViewCtrl.

The problem is that this is not quite enough because wxDataViewCtrl also
has native implementation in wxGTK and wxOSX, so implementing this method
really needs to be done in wxWidgets itself because it needs to be done
differently under different platforms.

Again, any contributions doing this would be welcome (please see our
guide at https://trac.wxwidgets.org/wiki/HowToSubmitPatches), but
unfortunately I don't think I'm going to have time to do this myself any
time soon.
Reply all
Reply to author
Forward
0 new messages