
Originally Posted by
high_flyer
You can subclass QMenu, and override its mousePressEvent() to trigger the action only on left clicks.
Hello every one,
i've the same problem with stefan , but not the chip mouse
i want to disable the function of right triggered action ,
but i can't find a way to solve that , following is my problem code
contextMenu.h
#ifndef CONTEXTMENU_H
#define CONTEXTMENU_H
#include <QMenu>
#include <QMouseEvent>
class contextMenu
: public QMenu{
Q_OBJECT
public:
signals:
// void leftMouseClicked();
protected:
{
if(event->button() == Qt::LeftButton)
event->accept();
// else
// event->ignore();
// emit leftMouseClicked();
// emit triggered(QAction *action);
}
{
if(event->button() == Qt::LeftButton)
event->accept();
// else
// event->ignore();
// emit leftMouseClicked();
// emit triggered(QAction *action);
}
};
#endif // CONTEXTMENU_H
#ifndef CONTEXTMENU_H
#define CONTEXTMENU_H
#include <QMenu>
#include <QMouseEvent>
class contextMenu : public QMenu
{
Q_OBJECT
public:
contextMenu(QWidget *parent = 0);
signals:
// void leftMouseClicked();
protected:
void mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
event->accept();
// else
// event->ignore();
// emit leftMouseClicked();
// emit triggered(QAction *action);
}
void mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
event->accept();
// else
// event->ignore();
// emit leftMouseClicked();
// emit triggered(QAction *action);
}
};
#endif // CONTEXTMENU_H
To copy to clipboard, switch view to plain text mode
contextMenu.cpp // nothing actually
#include <contextMenu.h>
contextMenu
::contextMenu(QWidget *parent
){
}
#include <contextMenu.h>
contextMenu::contextMenu(QWidget *parent)
: QMenu(parent)
{
}
To copy to clipboard, switch view to plain text mode
In test_paint.cpp
// show menu when right button clicked
{
if(y < window_h){
// QMenu menu(this);
contextMenu menu(this);
menu.addAction(rrAct);
menu.addAction(testAct);
menu.exec(event->globalPos());
}
}
void test_paint::createActions()
{
rrAct
= new QAction(tr
("&Rr"),
this);
rrAct->setCheckable(true);
connect(rrAct, SIGNAL(triggered()), this, SLOT(rr()));
testAct
= new QAction(tr
("&Test"),
this);
testAct->setCheckable(true);
connect(testAct, SIGNAL(triggered()), this, SLOT(test()));
}
// show menu when right button clicked
void test_paint::contextMenuEvent(QContextMenuEvent *event)
{
if(y < window_h){
// QMenu menu(this);
contextMenu menu(this);
menu.addAction(rrAct);
menu.addAction(testAct);
menu.exec(event->globalPos());
}
}
void test_paint::createActions()
{
rrAct = new QAction(tr("&Rr"), this);
rrAct->setCheckable(true);
connect(rrAct, SIGNAL(triggered()), this, SLOT(rr()));
testAct = new QAction(tr("&Test"), this);
testAct->setCheckable(true);
connect(testAct, SIGNAL(triggered()), this, SLOT(test()));
}
To copy to clipboard, switch view to plain text mode
now i'm running this program and pressed right button ,
but i can't do anything after contextMenu displayed on screen
can anyone give a more complete sample about
"How to reimplement QMenu"
any help will be appreciated!
Thanks 
hello high_flyer ,
first , thanks for your reply ,
// l_msg is a label , after the two button pressed , just show some string
// it was working properly , but i want to disable the "right button triggered"
void test_paint::test()
{
l_msg->setText("test button selected");
}
void test_paint::rr()
{
l_msg->setText("rr button selected");
}
void test_paint::test()
{
l_msg->setText("test button selected");
}
void test_paint::rr()
{
l_msg->setText("rr button selected");
}
To copy to clipboard, switch view to plain text mode
Bookmarks