Hello, very new to this. If someone could please point me the right direction (or offer a solution) it would be much appreciated.
I’m using Qt Creator to create the ui of an application. Using mingw on Windows 7 os… I do not have any MS software with compilers.
Within the application there is a modified tree view widget where I am trying to have a context menu appear when right-clicked… and at the location of the click. So far – with one exception – the menu always appears at the top left corner of my computer screen regardless of where the application appears or is moved. The one exception is when the menu coordinates are hard coded. How to make it appear where the mouse is clicked?
Code below:
#include "modqtreeview.h"
#include <QMouseEvent>
#include <QMenu>
#include <QPoint>
modQTreeView::modQTreeView(QWidget *parent) : QTreeView(parent)
{
installEventFilter(this);
}
bool modQTreeView::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::ContextMenu)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
QMenu *menu = new QMenu(this);
menu->addAction(new QAction("New",this));
menu->addAction(new QAction("Edit",this));
menu->addAction(new QAction("Delete",this));
//QPoint xy = QPoint(mouseEvent->pos().x(), mouseEvent->pos().y()); // same top left corner with this
//QPoint xy = QPoint(200, 500); // position hard coding worked
//menu->move(xy);
menu->pos() = mouseEvent->globalPos();
menu->exec();
return false;
}
else
return QTreeView::eventFilter(obj, event);
}
↧