user1089656
user1089656

Reputation:

How to get QGridLayout item by clicked QPoint?

I have grid of QFrames in QGridLayout and a popup menu with some actions, which are targeted on the cell where mouse right click happens. On the implementation of ContextMenuEvent I get clicked QPoint using common event->pos() but how I get access to my correct cell object by that point? Or is there some better alternative solution path for this purpose?

void X::contextMenuEvent(QContextMenuEvent* event)
{   // QPoint target = event->pos();
    // TODO: m_gridLayout-> ...
    // myDerivedCell->setSomething();
}

Upvotes: 0

Views: 678

Answers (1)

Amartel
Amartel

Reputation: 4286

There are a bunch of solutions here. The simplest is to go through your widgets, calling bool QWidget::underMouse () const. My favorite is this:

frame_i->setContextMenuPolicy(Qt::CustomContextMenu);
connect(frame_i, SIGNAL(customContextMenuRequested(QPoint))
        , SLOT(onContextMenu(QPoint)));

...

void X::onContextMenu(const QPoint &pos)
{
    QFrame *w = qobject_cast < QFrame * >(sender());
    ...
}

Upvotes: 1

Related Questions