Qt-interest Archive, April 2008
How to pop context menu?
Message 1 in thread
Hi all...
I've been trying ways to make my QTreeView subclass respond
correctly to context menu requests (right-clicking), but couldn't
seem to make things work correctly.
What I understand from the official examples is that to pop
context menus, I have to subclass the QTreeView and override
the contextMenuEvent(). Everything seemed fine up to that point.
The function responded to right-clicks.
My problem is how can I get the QModelIndex of the item the
user is right-clicking on? Since I need to pop a context menu
based on which item gets right-clicked.
I tried the code below but always get invalid QModelIndex.
Thanks before, and here's the code...
//---
void MyTreeView::contextMenuEvent(QContextMenuEvent* evt)
{
QPoint loc = evt->globalPos();
QModelIndex index = indexAt(loc);
qDebug() << index.isValid() ; // <<--- always returns false
// whichever item I
right-clicked on the view
}
//---
--
[ signature omitted ]
Message 2 in thread
Barkah Yusuf Widodo wrote:
> Hi all...
>
> I've been trying ways to make my QTreeView subclass respond
> correctly to context menu requests (right-clicking), but couldn't
> seem to make things work correctly.
See the "clicked" signal from QAbstractItemView. It contains the model
index already as a parameter.
Regards,
Marius K.
--
[ signature omitted ]
Message 3 in thread
The problem is your location is in global window coordinates...
From the docs:
QModelIndex QAbstractItemView::indexAt ( const QPoint & point ) const
[pure virtual]
Returns the model index of the item at the viewport coordinates point.
In the base class this is a pure virtual function.
You need to get the viewport for the view
Then convert using viewPort()->mapFromGlobal
Then run indexAt...
That said... clicked would work as well...
Scott
> -----Original Message-----
> From: Barkah Yusuf Widodo [mailto:barkah.yusuf@xxxxxxxxx]
> Sent: Tuesday, April 15, 2008 8:03 AM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: How to pop context menu?
>
> Hi all...
>
> I've been trying ways to make my QTreeView subclass respond
> correctly to context menu requests (right-clicking), but couldn't
> seem to make things work correctly.
>
> What I understand from the official examples is that to pop
> context menus, I have to subclass the QTreeView and override
> the contextMenuEvent(). Everything seemed fine up to that point.
> The function responded to right-clicks.
>
> My problem is how can I get the QModelIndex of the item the
> user is right-clicking on? Since I need to pop a context menu
> based on which item gets right-clicked.
>
> I tried the code below but always get invalid QModelIndex.
>
> Thanks before, and here's the code...
>
> //---
>
> void MyTreeView::contextMenuEvent(QContextMenuEvent* evt)
> {
> QPoint loc = evt->globalPos();
> QModelIndex index = indexAt(loc);
> qDebug() << index.isValid() ; // <<--- always returns false
> // whichever item I
> right-clicked on the view
> }
>
> //---
>
> --
> Barkah
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]
Message 4 in thread
On Tue, Apr 15, 2008 at 11:15 PM, Scott Aron Bloom
<Scott.Bloom@xxxxxxxxxxxx> wrote:
>
> From the docs:
> QModelIndex QAbstractItemView::indexAt ( const QPoint & point ) const
> [pure virtual]
> Returns the model index of the item at the viewport coordinates point.
>
> In the base class this is a pure virtual function.
I'm aware of this, I figured that the QTreeView has some sort of default
implementation for indexAt().
> The problem is your location is in global window coordinates...
> You need to get the viewport for the view
> Then convert using viewPort()->mapFromGlobal
>
> Then run indexAt...
Guess this is the cause. I'll just give it a quick fix...
Thank you.
--
[ signature omitted ]