PDA

View Full Version : Misplaced Context Menu



kandalf
16th February 2007, 02:55
Hi guys, I've been playing around with a context menu on a QListWidget and I can't make it work.
I have a slot connected to the customContextMenuRequested(const QPoint pos&) which creates a QMenu and opens it in "pos" (theoretically).
The thing is that I can't show the menu on the right place. It shows itself way too far from mouse, even when I debug the itemAt(pos)->text() and can see the right item is selected.
This is my code:

void AdminWindow::contextMenuRequested( const QPoint &pos )
{
if (tablesList->itemAt(pos))
{
QMenu popupMenu(tablesList);
connect( popupMenu.addAction("&Edit Table"), SIGNAL(triggered()), this, SLOT(editActionTriggered()) );
connect( popupMenu.addAction("&Drop Table"), SIGNAL(triggered()), this, SLOT(dropActionTriggered()) );
// qWarning(tablesList->itemAt(pos)->text().toAscii());

popupMenu.exec(pos + QPoint(tablesList->horizontalScrollBar()->value(), tablesList->verticalScrollBar()->value()));
}
}

I tried adding values from scrollbars as I saw in this forum, but no luck.
I also tried with mapToParent() and mapToGlobal(), same results.
Any ideas?

Thanks a lot in advance.

jrideout
16th February 2007, 04:03
Try

popupMenu.exec( mapToGlobal( pos ) );

kandalf
16th February 2007, 11:18
Try

popupMenu.exec( mapToGlobal( pos ) );

Sorry, already tried, but no luck.

Thanx anyway.

fullmetalcoder
16th February 2007, 12:44
Sorry, already tried, but no luck.
Then try the contrary :

if pos is in global coordinates :

popupMenu.exec( tablesList->mapFromGlobal(pos) );if pos is in local coordinates :

popupMenu.exec( tablesList->mapFromGlobal(mapToGlobal(pos)) );Hope this helps.

kandalf
18th February 2007, 00:25
Ok guys, I couldn't make it work. I reimplemented contextMenuEvent method and now it works as it should.

My code ended up like this:


TableListWidget.h
#include <QListWidget>

class TableListWidget : public QListWidget
{
Q_OBJECT
public:
TableListWidget(QWidget *);

void contextMenuEvent(QContextMenuEvent *);
};

TableListWidget.cpp
...
void TableListWidget::contextMenuEvent( QContextMenuEvent *event)
{
QMenu popupMenu(this);
connect( popupMenu.addAction("&Edit Table"), SIGNAL(triggered()), this, SLOT(editActionTriggered()) );
connect( popupMenu.addAction("&Drop Table"), SIGNAL(triggered()), this, SLOT(dropActionTriggered()) );
// qWarning(tablesList->itemAt(pos)->text().toAscii());

// popupMenu.exec(pos + QPoint(tablesList->horizontalScrollBar()->value(), tablesList->verticalScrollBar()->value()));
popupMenu.exec(this->mapToGlobal(event->pos()));
}
...


Thanx everybody for the answers anyway.

Cheers!

jpn
18th February 2007, 00:55
My code ended up like this:


popupMenu.exec(this->mapToGlobal(event->pos()));


By the way, QContextMenuEvent already contains a global position of the request so it could be even:

popupMenu.exec(event->globalPos());
:)

kandalf
18th February 2007, 04:28
popupMenu.exec(event->globalPos());
Thanx! I saw this method in docs and I thought I tried it and did not work. But, actually, I didn't, because it DOES work.

Thanx again.
Cheers.