Customizing a TreeView


#1

I needed to switch from using a Listbox to using a TreeView. In a test app, I simply added a TreeView to my main view component, and set its root item (and that item’s sub-items), then overrode paintItem in my derived TreeViewItem class.

So, next, in Projucer, I created a class derived from TreeView, in order to hold some other information (such as lo-res and hi-res bitmaps to support HiDPI), and dropped that on a component view (again, overriding the derived TreeViewItem’s paintItem() function and setting its root item, to the same item that my simple TreeView object used in my test app). But even though this class I derived from TreeView is created fine, and its root item is set, the individual items are never drawn.

The only difference I see from my test is that I’m using a derived class from TreeView, instead of directly using a TreeView object. Is there some function I need to override in order to get my derived class to behave the same as a simple TreeView instance would?


#2

Well, since that doesn’t work, I’m just adding a Listener to my derived TreeViewItem class, so that when painting or being clicked, it can call back to the class that contains the TreeView, which I set as the Listener, to do things like pick a background image for the item based on HiDPI or not, and to respond to selections of specific items to take actions based on the content of the clicked item. Seems to be working. (Still no idea why I can’t derive from the TreeView successfully, though.)


#3

Not sure, what could cause this.
Which methods in TreeView did you override?


#4

In the TreeView, I didn’t add any overrides myself. Projucer added paint() and resized(). In paint(), I tried doing nothing, or calling TreeView::paint(). Neither approach made a difference. (I only added some items that are unrelated to the base TreeView class.)

The functions that I overrode were in my class derived from TreeViewItem: mightContainSubItems() and paintItem().

As a test, I put both my TreeView-derived class and a plain TreeView object on my Component view, and the TreeView works but the derived class does not. Both are using the exact same derived TreeViewItem objects.

Not gonna work on this any more, since I have it working now using a TreeView and adding my view component as a “listener” to my TreeViewItem objects to handle the other items I need. (The reason I wanted a derived class was to make it completely portable to other containers. The “listener” pattern provides this almost as easily.)


#5

I think, I know what the problem is.
You wrote, that the Projucer also added “resized()”. You should not override “resized()” for the TreeView. Or if you do, you have to call TreeView::resized(). Otherwise the TreeViewItems are not drawn.


#6

Ah, ok. I hadn’t thought of messing with that, since Projucer added it, and my windows are not resizable. Thanks!


#7

Yeah, it can be tricky sometimes.

AFAIK: Resized is always called during the initialization of the application. Even if the application is not resizeable. Just so every component can get an initial size.