About ListBox::getComponentForRowNumber()


#1

Hello Jules,
I cannot understand why ListBox::getComponentForRowNumber() must return NULL if a row is offscreen.
Is there an alternative to reliably retrieve the Component that is linked to a ListBox row?


#2

[quote=“hladik”]Hello Jules,
I cannot understand why ListBox::getComponentForRowNumber() must return NULL if a row is offscreen.
Is there an alternative to reliably retrieve the Component that is linked to a ListBox row?[/quote]

The Listbox only creates enough Component to represent rows that are actually visible. It does NOT create a Component for each row logical! I have lists with 100,000+ elements…

What the Listbox does is shuffle these row Components around so they represent the rows that are on screen. The routine getComponentForRowNumber() necessarily returns NULL if the row is not at least partially visible, since there is no corresponding component.

What exactly is it that you are trying to accomplish? I’m sure it can be done a different way.


#3

Well, thanks, this is quite enlightening… I was expecting too much from the ListBox!


#4

What are you trying to do with it?


#5

I have some sliders that should control the object that corresponds to the currently selected row in the ListBox. My code is more or less like:[code]int selectedRow = theListBox->getSelectedRow();

if(selectedRow != -1)
{
Object* row = dynamic_cast<Object*>(theListBox->getComponentForRowNumber(selectedRow));

if(row != NULL)
{
…do something
}
}
[/code]


#6

well, if it’s offscreen, surely it wouldn’t need to be changed? And then when it comes on screen, it would have a component created for it, which would take whatever state you give it. I mean, I’m presuming you’re actually keeping the data and the row components separate [the row components ‘represent’ the data, and are not the actual container for it]; if you’re not (and you’re expecting the row component to stay alive and keep your data) then you need to rethink your model.


#7

The object that I put into a row is a placeholder that keeps just a few data about the real, heavy-weight object it represents. It uses these data to graphically represent its counterpart and ideally it should also be able to inform it about user interface changes.
The problem here is that if I can’t retrieve the placeholder corresponding to the selected row, I have no means to determine which heavy-weight object is selected.


#8

Well how would the placeholder have been put there in the first place if it weren’t for a row number indicating the data it should represent? Surely the row number is all you need to know what it would be referring to.


#9

You should never, ever rely on a GUI class to store data that can’t be obtained directly from your data model, that’s definitely an indication that your design’s not good.


#10

There is not necessarily a one-to-one relationship between the rows count/order and the model objects.
One solution, that I don’t particularly like, is to put a row number info back into the heavy-weight object whenever I setup a new placeholder.