DataViewCtrl: how to get the item under the mouse cursor? (hit test)

594 views
Skip to first unread message

dario...@gmail.com

unread,
Mar 5, 2014, 10:12:23 AM3/5/14
to wxpytho...@googlegroups.com
I'm trying to compute the DataViewItem that is under the mouse cursor: in ListCtrl that is done through the HitTest method, but the corresponding method for DataViewCtrl seems to behave differently, and I haven't understood how to use it. The following script, using a DataViewListCtrl, is as far as I've managed to get, can anybody help me? Thank you.

#########################################
import wx
import wx.dataview

def test():
    pos = view.ScreenToClient(wx.GetMousePosition())
    item = view.RowToItem(0)
    col = view.GetColumn(0)
    print(col.__class__)
    # How to get the item under the mouse cursor?
    print(view.HitTest(pos, item, col))
    #timer.Restart()

app = wx.App()
frame = wx.Frame(None)
view = wx.dataview.DataViewListCtrl(frame, style=wx.dataview.DV_MULTIPLE)
column = view.AppendTextColumn("Text", 0)
view.AppendItem(('Test1', ))
view.AppendItem(('Test2', ))
view.AppendItem(('Test3', ))
timer = wx.CallLater(3000, test)
frame.Centre()
frame.Show()
app.MainLoop()
#########################################

Werner

unread,
Mar 5, 2014, 10:45:37 AM3/5/14
to wxpytho...@googlegroups.com
Hi,

On 05/03/2014 16:12, dario...@gmail.com wrote:
> I'm trying to compute the DataViewItem that is under the mouse cursor:
> in ListCtrl that is done through the HitTest method, but the
> corresponding method for DataViewCtrl seems to behave differently, and
> I haven't understood how to use it. The following script, using a
> DataViewListCtrl, is as far as I've managed to get, can anybody help
> me? Thank you.
I gave it a shot and with 2.9.5 I get this exception:

File "h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py", line 26,
in <module>
app.MainLoop()
File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\_core.py", line
8660, in MainLoop
wx.PyApp.MainLoop(self)
File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\_core.py", line
7952, in MainLoop
return _core_.PyApp_MainLoop(*args, **kwargs)
File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\_misc.py", line
1367, in Notify
self.notify()
File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\_core.py", line
16870, in Notify
self.result = self.callable(*self.args, **self.kwargs)
File "h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py", line 12,
in test
print(view.HitTest(pos, item, col))
File "c:\Python27\Lib\site-packages\wx-2.9.5-msw\wx\dataview.py", line
1817, in HitTest
return _dataview.DataViewCtrl_HitTest(*args, **kwargs)

TypeError: in method 'DataViewCtrl_HitTest', expected argument 4 of type
'wxDataViewColumn *&'

even though 'col' at that time is an instance of 'wx.dataview.DataViewColumn

If I run it against Phoenix (3.0.1.dev75864) I get:

File "h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py", line 28,
in <module>
app.MainLoop()
File "c:\Python27\Lib\site-packages\wx-3.0.1-msw-phoenix\wx\core.py",
line 1878, in MainLoop
rv = wx.PyApp.MainLoop(self)
File "c:\Python27\Lib\site-packages\wx-3.0.1-msw-phoenix\wx\core.py",
line 1940, in Notify
self.notify()
File "c:\Python27\Lib\site-packages\wx-3.0.1-msw-phoenix\wx\core.py",
line 3017, in Notify
self.result = self.callable(*self.args, **self.kwargs)
File "h:\devProjectsT\aaTests\aaMisc\dariogiova\dvchitest.py", line 14,
in test
print(view.HitTest(pos, item, col))

TypeError: DataViewCtrl.HitTest(): too many arguments

So, looks like there is a problem with HitTest.

Could you use instead 'EVT_DATAVIEW_ITEM_ACTIVATED'?

Werner

dario...@gmail.com

unread,
Mar 5, 2014, 11:01:47 AM3/5/14
to wxpytho...@googlegroups.com

Sorry, I should have mentioned that I'm using 3.0.0, which raises the same exception as 2.9.5. Is it a known bug? The only related report I've found is http://trac.wxwidgets.org/ticket/12582 which seems farily old, though still open.

Should 'EVT_DATAVIEW_ITEM_ACTIVATED' be trieggered when the mouse hovers over an item? The documentation says "This event is triggered by float clicking an item or pressing some special key (usually “Enter”) when it is focused": I don't know what "float clicking" is, but the only way I can trigger it seems to be by double-clicking the item, which is very far from what I need. What I'm trying to do is actually temporarily work around another bug, http://trac.wxwidgets.org/ticket/13531 while waiting for 3.0.1 to be released.

Werner

unread,
Mar 5, 2014, 11:07:25 AM3/5/14
to wxpytho...@googlegroups.com
Hi,

On 05/03/2014 17:01, dario...@gmail.com wrote:
>
>
> On Wednesday, March 5, 2014 11:45:37 PM UTC+8, werner wrote:
...
> Sorry, I should have mentioned that I'm using 3.0.0, which raises the
> same exception as 2.9.5. Is it a known bug?
It looks like but I am not sure.

Yes, providing the version one uses is always good and if you get an
exception you should include it too, as often that is enough to help.

...
> Should 'EVT_DATAVIEW_ITEM_ACTIVATED' be trieggered when the mouse
> hovers over an item? The documentation says "This event is triggered
> by float clicking an item or pressing some special key (usually
> “Enter”) when it is focused": I don't know what "float clicking" is,
> but the only way I can trigger it seems to be by double-clicking the
> item, which is very far from what I need. What I'm trying to do is
> actually temporarily work around another bug,
> http://trac.wxwidgets.org/ticket/13531 while waiting for 3.0.1 to be
> released.
No it will not trigger on mouse hover, only when an item is 'selected',
most of the time that is when the user would expect something to happen.

Werner

dario...@gmail.com

unread,
Mar 5, 2014, 11:15:08 AM3/5/14
to wxpytho...@googlegroups.com


On Thursday, March 6, 2014 12:07:25 AM UTC+8, werner wrote:
Hi,

On 05/03/2014 17:01, dario...@gmail.com wrote:
>
>
> On Wednesday, March 5, 2014 11:45:37 PM UTC+8, werner wrote:
...
> Sorry, I should have mentioned that I'm using 3.0.0, which raises the
> same exception as 2.9.5. Is it a known bug?
It looks like but I am not sure.

Yes, providing the version one uses is always good and if you get an
exception you should include it too, as often that is enough to help.


I see, thanks for your help. I thought it was me doing something wrong. Here's my exception:
########################
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/wx-3.0-gtk2/wx/_misc.py", line 1367, in Notify
    self.notify()
  File "/usr/lib/python2.7/site-packages/wx-3.0-gtk2/wx/_core.py", line 16866, in Notify
    self.result = self.callable(*self.args, **self.kwargs)
  File "test_dvlistc3.py", line 10, in test
    print(view.HitTest(pos, item, col))
  File "/usr/lib/python2.7/site-packages/wx-3.0-gtk2/wx/dataview.py", line 1817, in HitTest

    return _dataview.DataViewCtrl_HitTest(*args, **kwargs)
TypeError: in method 'DataViewCtrl_HitTest', expected argument 4 of type 'wxDataViewColumn *&'
########################

My system is:
Arch Linux x86_64
GTK+ 2.24.22
wxGTK 3.0.0
wxPython 3.0.0.0

Robin Dunn

unread,
Mar 15, 2014, 12:51:43 AM3/15/14
to wxpytho...@googlegroups.com
Werner wrote:
> Hi,
>
> On 05/03/2014 17:01, dario...@gmail.com wrote:
>>
>>
>> On Wednesday, March 5, 2014 11:45:37 PM UTC+8, werner wrote:
> ...
>> Sorry, I should have mentioned that I'm using 3.0.0, which raises the
>> same exception as 2.9.5. Is it a known bug?
> It looks like but I am not sure.

Sorta. The method wrapper will need to be rewritten (Pythonized) to
return the item and col as a tuple instead of expecting to pass by
reference through the parameters.

--
Robin Dunn
Software Craftsman
http://wxPython.org

nepix32

unread,
Jul 1, 2014, 10:49:09 AM7/1/14
to wxpytho...@googlegroups.com

On Saturday, March 15, 2014 5:51:43 AM UTC+1, Robin Dunn wrote:
like but I am not sure.

Sorta. The method wrapper will need to be rewritten (Pythonized) to
return the item and col as a tuple instead of expecting to pass by
reference through the parameters.
I am trying to use DataViewCtrl and I am quite happy with it.
Drag and Drop seems to work as well, but now I struggle to find on which DataViewItem the drop target is.
 
As mentioned above, HitTest throws this error:
 
TypeError: in method 'DataViewCtrl_HitTest', expected argument 4 of type 'wxDataViewColumn *&' 
 
because this is not wrapped yet.
 
Can I workaround this by providing a memoryview/buffer/pointer myself until this gets fixed?
 
With best regards

Nathan McCorkle

unread,
Jul 9, 2014, 2:32:53 PM7/9/14
to wxpytho...@googlegroups.com
hmm, I tried passing col.this, as well as trying int(col.this) with no luck. 
:(

nepix32

unread,
Oct 7, 2014, 6:58:37 AM10/7/14
to wxpytho...@googlegroups.com


On Saturday, March 15, 2014 5:51:43 AM UTC+1, Robin Dunn wrote:
Sorta. The method wrapper will need to be rewritten (Pythonized) to
return the item and col as a tuple instead of expecting to pass by
reference through the parameters.

In  wxPython/trunk/src/dataview.i in line 2419 there is already the telling comment:
// TODO:  Should probably change this to return the item and col as a tuple...)
virtual void HitTest( const wxPoint & point, wxDataViewItem &item, wxDataViewColumn* &column ) const;

As I have no idea whatsoever how to tell SWIG how to do that, can you give a short explanation what to change in the SWIG-file to get the behaviour in the comment, or is this very difficult to describe?
With thanks in advance

Robin Dunn

unread,
Oct 13, 2014, 8:17:29 PM10/13/14
to wxpytho...@googlegroups.com
nepix32 wrote:
>
>
> On Saturday, March 15, 2014 5:51:43 AM UTC+1, Robin Dunn wrote:
>
> Sorta. The method wrapper will need to be rewritten (Pythonized) to
> return the item and col as a tuple instead of expecting to pass by
> reference through the parameters.
>
>
> In wxPython/trunk/src/dataview.i
> <http://trac.wxwidgets.org/browser/wxPython/trunk/src/dataview.i?rev=70457#L2419>
> in line 2419 there is already the telling comment:
> ||
> // TODO: Should probably change this to return the item and col as a
> tuple...)
> virtualvoidHitTest(constwxPoint &point,wxDataViewItem
> &item,wxDataViewColumn*&column )const;
>
> As I have no idea whatsoever how to tell SWIG how to do that, can you
> give a short explanation what to change in the SWIG-file to get the
> behaviour in the comment, or is this very difficult to describe?


Thanks for the reminder. I've just implemented it for the CLassic 3.0.2
release.

Dario Giovannetti

unread,
Oct 14, 2014, 10:39:24 AM10/14/14
to wxpytho...@googlegroups.com
On 14/10/14 08:17, Robin Dunn wrote:
>
> Thanks for the reminder. I've just implemented it for the CLassic
> 3.0.2 release.
>

Thank you Robin, does that fix http://trac.wxwidgets.org/ticket/12582 ?
And, if I understand well, would that provide a way to get the current
scroll position of the DataViewCtrl by using the top coordinates of that
window? (It was the only problem left unsolved in
https://groups.google.com/d/msg/wxpython-users/isAboUxaoFs/z9PRJClSPgwJ )
Reply all
Reply to author
Forward
0 new messages