PDA

View Full Version : How to disable right click on web page



swarupa
7th March 2016, 18:08
Hi all

How to disable right click on web page which is displayed through QWebEngineView in qt?

anda_skoa
7th March 2016, 22:38
You could try with an event filter, see QObject::eventFilter().

Cheers,
_

swarupa
8th March 2016, 02:10
Hi

Thanks for your answer..

Sorry iam a neewbie, could you explain me little bit more eloborate... so that it will be useful to me..

Thanks.

anda_skoa
8th March 2016, 09:53
An event filter allows you to see all events going to another object and deciding whether you want the event to reach the recipient or not.

You need a (direct or indirect) subclass of QObject, implement the eventFilter() method, make it return true for the right mouse button events and install that object on the web engine view as its event filter.

Cheers,
_

swarupa
8th March 2016, 15:46
webengview->installEventFilter(this);



bool My_class::eventFilter( QObject * obj, QEvent * event )
{
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent* m = (QMouseEvent*) event;

m->ignore();
return true;
}

bool val= QObject::eventFilter(obj, event);

QWebEngineView * menu = dynamic_cast<QWebEngineView*>(obj);

if(menu && event->type() == QEvent::MouseButtonPress)
{
QMouseEvent * ev = dynamic_cast<QMouseEvent*>(event);
if(ev)
{
if(ev->button() == Qt::RightButton)
{
ev->ignore();
return false; // yes we filter the event
}

}
}
return val;

}

Hi

I have implemented as shown above but still iam unable to avoid mouse right click.. I am i doing any thing wrong.. please let me know..

Thanks,

Added after 32 minutes:

I have tried with the following code by using event filter but i cant disable the right click


webengview->installEventFilter(this);



bool My_class::eventFilter( QObject * obj, QEvent * event )
{
bool val= QObject::eventFilter(obj, event);

QWebEngineView * menu = dynamic_cast<QWebEngineView*>(obj);

if(menu && event->type() == QEvent::MouseButtonPress)
{
QMouseEvent * ev = dynamic_cast<QMouseEvent*>(event);
if(ev)
{
if(ev->button() == Qt::RightButton)
{
ev->ignore();
return false; // yes we filter the event
}

}
}
return val;

}

anda_skoa
8th March 2016, 15:57
Maybe the view is not the recipient after all.

Install the event filter in the QApplication object and check which object gets the mouse press when you press on the view.

Cheers,
_

nopcode85
7th September 2016, 12:25
QWebEngineView is derived from QWidget, so you can use

setContextMenuPolicy( Qt::NoContextMenu )
or

setContextMenuPolicy( Qt::PreventContextMenu )
to disable the right clicks. Both behave slightly differently and pass the mouse clicks in different ways to the underlying WebPage.