简体   繁体   中英

How to implement virtual scrolling in wxwidgets

I am using wxScrolledWindow to display data(in rows and columns form). For example, I have 4 columns and around 1200 rows(all of which have the same height). Say first column represents name, 2nd column represents phone number, and so on. This works well but the problem is that since i've added all the 1200 rows into the wxScrolledWindow the program is consuming large amount of memory.

So, I want that only the rows that are visible to the user should be present at a given time inside the wxScrollWindow and when the user starts scrolling the other rows should be dynamically created. After searching for this I came to know that this is called virtual scrolling and wxWidgets offers this in wxListCtrl using the wxLC_VIRTUAL style. But the problem with this is that i cannot customize the appearance of wxListCtrl . For example, say the 3rd column in my custom scrollist contains a button with some text and the 4th column contains some wxStaticText and i want to change the height, background colour of each row meanwhile maintaining virtual scrolling.

So my questions are:

  1. Is there some documentation where i can read the theory of virtual scrolling(on which wxListCtrl 's virtual scroll was based on) so that i can implement it by myself. Note that i have read the github repo of wxWidgets but from there i couldn't figure out and also i feel the the documentation there is lacking.

  2. Is there a working demo of implementing virtual scrolling in wxwidgets for a wxScrollWindow or for any other window. I mean just so that I can use it as a reference and starting point.

PS: I have searched the web for the same but couldn't find any post that shows how we can implement virtual scrolling in wxwidgets so I decided to ask It myself.

There is no such thing as "virtual scrolling", which is why you haven't found anything explaining how to do it. The wxLC_VIRTUAL flag reuses the old Microsoft terminology to call controls not storing their data fully "virtual", but this is only very peripherally related to scrolling.

Scrolling itself is implemented simply by drawing the "logical canvas", which may be much bigger than the "physical view" of it, which is the window, at the offset determined by the scrollbars position. So the only thing to do is to set this offset correctly, which is done by PrepareDC() function of wxScrolledCanvas or wxScrolledWindow , both of which are actually specializations of wxScrolled<> template. After passing your wxPaintDC to this function, you should simply draw on it as usual (well, maybe optimizing by not drawing the parts that are outside of the visible area) without caring about scrolling at all -- and yet it will work, all on its own. You should look at the scroll sample to see how exactly this works.

Note that wxScrolled<> assumes that all your rows have the same height. If this is not the case, you can use wxVScrolledWindow ("V" means "variable", as in rows of variable height) or the already mentioned wxVListBox , which inherits from it.

The simplest possible class allowing decent customization possibilities is wxSimpleHtmlListBox .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM