#1 Incorporating an OLV in XRC

Phillip Piper
Feature (10)

I tried to put an OLV on a panel using XRCed, by subclassing a ListCtrl as ObjectListView.ObjectListView. This fails when trying to load the XRC. Robin suggested that the problem might be that OLV doesn't work well with 2-phase creation, which all standard wx controls are set up for. Here's a quote from him on the wxpython-users list, in a message of 26 Sep 2008 at 11:07:

The general recipe for using a Python class as an XRC subclass is that the init needs to call the PreFoo() function to create the instance, instead of calling Foo.init like normal. In the demo it is done like this:

class MyCustomPanel(wx.Panel):
def init(self):
p = wx.PrePanel()

the Create step is done by XRC.

self.Bind(wx.EVT_WINDOW_CREATE, self.OnCreate)
self.Bind(wx.EVT_SIZE, self.OnSize)

Note that anything that requires that the UI widget exists can not be done in init. That is why the sample binds the EVT_WINDOW_CREATE event, so it can do that UI initialization in that handler. Also, the PostCreate method is simply a bit of Python code that does some magic to transplant the guts of the p instance into self.

To use OLV as an XRC subclass might take a little bit of refactoring. (I haven't looked at the OLV code in any detail yet, so all of this is just generalization.) First everything in the init except for the call to wx.ListCtrl.init should be moved into a separate method, such as doInit or something, and then a call to doInit could be added to init so nothing is changed functionality-wise when the class is used normally. Second there needs to be a way to create an instance of the OLV that does not result in a call to x.ListCtrl.init but uses wx.PreListCtrl instead. The easiest way to do that is to derive a subclass and not call OLV's init. Instead it could do something like the above and then call the doInit() method you created in the first step from the OnCreate event handler.


  • Phillip Piper
    Phillip Piper

    Thanks for the request. I'll look into doing it since it seems like a generally useful ability.

  • Phillip Piper
    Phillip Piper

    • status: open --> closed
    • Group: --> Next_Release_(example)