wxDataViewListCtrl getting column of selected row Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
sivaranjani
Knows some wx things
Knows some wx things
Posts: 46
Joined: Wed Mar 14, 2018 10:43 am

wxDataViewListCtrl getting column of selected row

Post by sivaranjani »

Hi,
I am using wxDataViewListCtrl and event used is EVT_DATAVIEW_SELECTION_CHANGED.From this event,I would like to get column index for the selected row.

I have tried using the below code, where evt.GetColumn() returns me -1 and when I try to use evt.GetDataViewColumn()->GetModelColumn();, it crashes. I need to know if I am following the right steps to get the current column or is there anything else I need to be doing?

Code: Select all

	int row = lc->GetSelectedRow();
	int col = evt.GetColumn();
	wxDataViewColumn *col = lc->GetCurrentColumn();
	int colno =lc->GetColumnPosition(col);
	int col = evt.GetDataViewColumn()->GetModelColumn();
Thanks.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl getting column of selected row

Post by doublemax »

A SELECTION_CHANGED event doesn't carry information about the column.

Try wxDataViewCtrl::HitTest to find the column under the mouse pointer.
Use the source, Luke!
sivaranjani
Knows some wx things
Knows some wx things
Posts: 46
Joined: Wed Mar 14, 2018 10:43 am

Re: wxDataViewListCtrl getting column of selected row

Post by sivaranjani »

Hi,
I have tried using wxDataViewCtrl::HitTest,but column index always returning first index of the column.Is there any other solution?

Thanks
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl getting column of selected row

Post by doublemax »

I tested with this code in the "dataview" sample and it worked:

Code: Select all

void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
{
  wxDataViewCtrl *dvc = m_ctrl[0];
  wxDataViewItem item;
  wxDataViewColumn *col;
  dvc->HitTest( dvc->ScreenToClient(::wxGetMousePosition()), item, col );
  if( col != NULL )
    wxLogMessage( "column hit: %d %s", col->GetModelColumn(), col->GetTitle() );
  else
    wxLogMessage("no column found");
Use the source, Luke!
happythings
In need of some credit
In need of some credit
Posts: 2
Joined: Thu Mar 12, 2020 9:53 am

Re: wxDataViewListCtrl getting column of selected row

Post by happythings »

sivaranjani wrote: Thu Mar 15, 2018 6:12 am Hi,
I have tried using wxDataViewCtrl::HitTest,but column index always returning first index of the column.Is there any other solution?

Thanks
doublemax wrote: Thu Mar 15, 2018 9:08 am I tested with this code in the "dataview" sample and it worked:

Code: Select all

void MyFrame::OnSelectionChanged( wxDataViewEvent &event )
{
  wxDataViewCtrl *dvc = m_ctrl[0];
  wxDataViewItem item;
  wxDataViewColumn *col;
  dvc->HitTest( dvc->ScreenToClient(::wxGetMousePosition()), item, col );
  if( col != NULL )
    wxLogMessage( "column hit: %d %s", col->GetModelColumn(), col->GetTitle() );
  else
    wxLogMessage("no column found");

The Real Problem is :
When drag the the horizon scroll bar in wxDataViewCtrl to the right side columns and some left column(s) invisible, HitTest will return wrong column.
How to solve?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl getting column of selected row

Post by doublemax »

When drag the the horizon scroll bar in wxDataViewCtrl to the right side columns and some left column(s) invisible, HitTest will return wrong column.
At least in the generic implementation of wxDataViewCtrl (which is only used under Windows), this works correctly.

What platform are you using?
Use the source, Luke!
happythings
In need of some credit
In need of some credit
Posts: 2
Joined: Thu Mar 12, 2020 9:53 am

Re: wxDataViewListCtrl getting column of selected row

Post by happythings »

my os is ubuntu 16.04 LTS

i tested same codes on windows, it's OK.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl getting column of selected row

Post by doublemax »

I tested this under Linux and i think it's a bug. Please open a ticket at http://trac.wxwidgets.org
Use the source, Luke!
zybertanc
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Mar 21, 2024 1:59 pm

Re: wxDataViewListCtrl getting column of selected row

Post by zybertanc »

Hi,

recently I'm implementing a context_menu triggered by right click in the DataViewListCtrl and therefore I find this post. I have similar issue when getting the column of the selected item.

The context menu that pops up when I right click on an item has certain width, and the Hittest() could return a wrong column index (which is on the right of the selected item). I understand why this happens but is there a solution that in the context_menu I can still get the correct column of the selected item?

Thanks.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxDataViewListCtrl getting column of selected row

Post by ONEEYEMAN »

Hi,
What OS?

Thank you.
zybertanc
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Mar 21, 2024 1:59 pm

Re: wxDataViewListCtrl getting column of selected row

Post by zybertanc »

ONEEYEMAN wrote: Thu Mar 21, 2024 4:42 pm Hi,
What OS?

Thank you.
Hi,

I'm working on Windows.

Thank you
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl getting column of selected row

Post by doublemax »

zybertanc wrote: Thu Mar 21, 2024 5:20 pm I'm working on Windows.
In that case the code from viewtopic.php?p=182658#p182658 should work. Did you try it?

If yes, what are the exact circumstances under which this does not work?
Use the source, Luke!
zybertanc
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Mar 21, 2024 1:59 pm

Re: wxDataViewListCtrl getting column of selected row

Post by zybertanc »

doublemax wrote: Thu Mar 21, 2024 5:55 pm
zybertanc wrote: Thu Mar 21, 2024 5:20 pm I'm working on Windows.
In that case the code from viewtopic.php?p=182658#p182658 should work. Did you try it?

If yes, what are the exact circumstances under which this does not work?
When I right click on an item of the DataViewListCtrl and a context menu pops, and then I click a item of the menu, at this moment Hittest() (it works) returns the position of the mouse cursor, which could be eventually not where the item of the DataViewListCtrl covers, but the one on its right or below it, this makes me get the wrong column. What I need is still the selected item of the DataViewListCtrl when I act in the poped context menu.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxDataViewListCtrl getting column of selected row

Post by doublemax »

You need to determine the item and column before you show the popup menu. Use wxWindow::GetPopupMenuSelectionFromUser() to show the popup. It's a blocking call, so when it returns, you still have the information about the selected item.
https://docs.wxwidgets.org/trunk/classw ... 682a61a93e
Use the source, Luke!
zybertanc
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Mar 21, 2024 1:59 pm

Re: wxDataViewListCtrl getting column of selected row

Post by zybertanc »

doublemax wrote: Fri Mar 22, 2024 9:14 am You need to determine the item and column before you show the popup menu. Use wxWindow::GetPopupMenuSelectionFromUser() to show the popup. It's a blocking call, so when it returns, you still have the information about the selected item.
https://docs.wxwidgets.org/trunk/classw ... 682a61a93e
Thank you for this tips. It now works and I get what is expected.
Post Reply