PDA

View Full Version : QWebView and right click reload



sky
15th March 2011, 12:36
Hi,

I have been trying to disable the right click "reload" menu on QWebView. After many failed attempts I tried the following code:



MyApplication::MyApplication(int argc, char *argv[]) :
QApplication(argc, argv)
{
installEventFilter(this);
}

bool MyApplication::eventFilter(QObject *obj, QEvent *e)
{
if ( ( e->type() == QEvent::MouseButtonPress )||
( e->type() == QEvent::MouseButtonRelease )||
( e->type() == QEvent::MouseButtonDblClick ) )
{
return true;
//QMouseEvent* m = static_cast<QMouseEvent *>(e);
//int i = m->button();
//if ( i == Qt::RightButton)
//{
// return true;
//}
}
else if ( e->type() == QEvent::KeyPress )
{
QKeyEvent* k = static_cast<QKeyEvent *>(e);
if(k->key() == Qt::Key_F4)
{
quit();
return true;
}
}
return QApplication::eventFilter(obj, e);
}


The strange thing is even though the mouse button events are disabled, still the "reload" menu shows up on the QWebView. I tired to inherit the QwebView and install my event filter also but with the same result. I think I missing something about this "reload" menu on QwebView.

Anyone, please let me know if you know of a way to disable the right click "reload" menu on QWebView. Thank you.

Sky.

agarny
15th March 2011, 13:04
What about using your own context menu? That's what I personally do:
myBrowser->setContextMenuPolicy(Qt::CustomContextMenu);

sky
16th March 2011, 06:09
Hi Agarny,

Thank you. That worked like a charm. I guess from next time.. I will post before I try.. :-).
But.. I still don't get it. I mean when I am blocking all the mouse events how can the browser respond to it with a context menu. I am surely missing something here.

Sky.

Added after 50 minutes:

Hey Agarny,

Your method works well.. But on flash ( like on youtube videos ) the right click context menu still shows up.. Would you have any ideas how to prevent that too..

Sky.

agarny
16th March 2011, 08:13
But.. I still don't get it. I mean when I am blocking all the mouse events how can the browser respond to it with a context menu. I am surely missing something here.You might want to check the documentation. If I recall correctly QWebBrowser will, by default, use its own context menu, no matter what, hence you should use setContextMenuPolicy if you don't want that default behaviour.


Your method works well.. But on flash ( like on youtube videos ) the right click context menu still shows up.. Would you have any ideas how to prevent that too..Sorry, but I wouldn't know about this. I imagine it might be a bug with Qt?

squidge
16th March 2011, 08:52
Thank you. That worked like a charm. I guess from next time.. I will post before I try.. :-).
But.. I still don't get it. I mean when I am blocking all the mouse events how can the browser respond to it with a context menu. I am surely missing something here.
Why not install the event filter on the webview itself rather than your application? Eg.



mWebView->installEventFilter(this);

sky
17th March 2011, 06:24
Hi Squidge,

Thank you for your suggestion. I implemented it and below is the code. But I was not able to get it to disable the right click on flash videos. And also I ran it through the debugger with a break point in the eventFilter method. And it looks like this eventFilter is never being called.

It looks like as if the events in QT are broadcasted to all the QObjects in the application. Also please do let me know if you know of any good literature that explains qt event mechanism in detail. Thank you.

Sky




Browser::Browser(QWidget *parent)
: QWebView(parent), m_page(this), m_pXMLHttp(NULL)
{

installEventFilter(this);

setPage(&m_page);
m_pFrame = page()->mainFrame();

m_page.installEventFilter(this);
m_pFrame->installEventFilter(this);
}

bool Browser::eventFilter(QObject *obj, QEvent *event)
{
if ( (event->type() == QEvent::MouseButtonPress )||
(event->type() == QEvent::MouseButtonRelease)||
( event->type() == QEvent::MouseButtonDblClick ) )
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (mouseEvent->button() == Qt::RightButton)
{
mouseEvent->accept();
return true;
}
}
//return QWebView::eventFilter(obj, event);
return false;
}

smike
23rd August 2012, 08:47
The best way if you want just to remove the "Reload" option from standart web view context menu is to use the following:

myWebView->page()->action(QWebPage::Reload)->setVisible(false);

(myWebView type is QWebView*)

If you want to hide any other standart context menu items just look at enum QWebPage::WebAction members

Hope this helps.

garibaldi76
18th May 2013, 18:39
I just got into the same problem but I also want to make the right click menu dynamically available. I still want to enable the right click menu just for debug purpose. In my case, calling setContextMenuPolicy() was a little awkward so I decided to give installEventFilter() one more try:



bool MyObject::eventFilter( QObject *watched, QEvent *event )
{
if( NULL != event )
{
QEvent::Type type = event->type();
if( ! m_bMouseEnabled &&
( type==QEvent::MouseButtonDblClick ||
type==QEvent::MouseButtonPress ||
type==QEvent::MouseButtonRelease ||
type==QEvent::MouseMove ||
type==QEvent::MouseTrackingChange ||
type==QEvent::ContextMenu ||
type==QEvent::GraphicsSceneContextMenu )
)
{
return true;
}
}
return false;
}

void WebKitView::initialize()
{
m_webView->installEventFilter( m_myObj );
}


Adding the checking of QEvent::ContextMenu and QEvent::GraphicsSceneContextMenu made it work in my application. I am not sure if this works for flash, tough.

viengelm
14th September 2016, 11:07
In a talk by one of the google developers of chromium - about the architecture of chromium,
he said that flash talks directly to the OS behind the scenes. It acquires its own HWND etc,
so flash probably gets the right-click directly from the OS, bypassing Qt.