[wxPython-users] Grid Cell Renderer

Robin Dunn robin at alldunn.com
Tue Feb 5 14:55:21 PST 2008


Mark Erbaugh wrote:
> On Wed, 2008-01-02 at 17:00 -0800, Robin Dunn wrote:
>> Mark Erbaugh wrote:
>>> On Wed, 2008-01-02 at 06:58 +0100, Raffaello Barella wrote:
>>>> Could you please tell what kinds of data you want to convert to a
>>>> string?
>>>>
>>> The data is an integer that corresponds to the string. I have a dict
>>> that has the integer as the key and the string as the value.
>>>
>>> wxPython has a GridCellEnumRenderer but I can't seem to get that working
>>> and other than it's existence, I can't find any documentation on it.
>>> It's not listed in the wxWidgets documentation, although it is there in
>>> the C++ code.
>> If you really want to keep the integer values all the way through then 
>> try implementing GetValueAsLong(self, row, col) in the table, and also 
>> CanGetValueAs(self, row, col, typeName) and return true for that cell 
>> for typeName == "long".  Otherwise the grid, editors, and renderers 
>> should translate the value to/from strings as needed.
> 
> 
> I'm having no luck implementing GetValueAsLong.  I added CanGetValueAs
> and GetValueAsLong methods  to my grid table.  I placed a print
> statement in it so I can see that it is being called and it returns a
> Python True.  However, the table's GetValueAsLong is not being called.
> Instead, the table's GetValue is called.  


I totally forgot that I did this.  The GetValueAsLong (Double and Bool 
too) map their calls to the Python GetValue, and then just ensures that 
the proper type of object was returned from GetValue.  In other words, 
the C++ GetValueAsLong will map to a call to the Python GetValue, and 
then will ensure that the return value is a number and convert it to a 
long for the C++ return value.  Here is the code:

     long GetValueAsLong( int row, int col ) {
         long rval = 0;
         wxPyBlock_t blocked = wxPyBeginBlockThreads();
         if (wxPyCBH_findCallback(m_myInst, "GetValue")) {
             PyObject* ro;
             PyObject* num;
             ro = wxPyCBH_callCallbackObj(m_myInst, 
Py_BuildValue("(ii)", row, col));
             if (ro && PyNumber_Check(ro)) {
                 num = PyNumber_Int(ro);
                 if (num) {
                     rval = PyInt_AsLong(num);
                     Py_DECREF(num);
                 }
                 Py_DECREF(ro);
             }
         }
         wxPyEndBlockThreads(blocked);
         return rval;
     }


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list