QWebView and right click reload
Hi,
I have been trying to disable the right click "reload" menu on QWebView. After many failed attempts I tried the following code:
Code:
MyApplication::MyApplication(int argc, char *argv[]) :
{
installEventFilter(this);
}
{
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 ) {
if(k->key() == Qt::Key_F4)
{
quit();
return true;
}
}
}
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.
Re: QWebView and right click reload
What about using your own context menu? That's what I personally do:
Code:
myBrowser->setContextMenuPolicy(Qt::CustomContextMenu);
Re: QWebView and right click reload
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.
Re: QWebView and right click reload
Quote:
Originally Posted by
sky
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.
Quote:
Originally Posted by
sky
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?
Re: QWebView and right click reload
Quote:
Originally Posted by
sky
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.
Code:
mWebView->installEventFilter(this);
Re: QWebView and right click reload
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
Code:
: 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);
}
{
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;
}
Re: QWebView and right click reload
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.
Re: QWebView and right click reload
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:
Code:
{
if( NULL != event )
{
QEvent::Type type
= event
->type
();
if( ! m_bMouseEnabled &&
( type
==QEvent::MouseButtonDblClick ||
type
==QEvent::MouseButtonPress ||
type
==QEvent::MouseButtonRelease ||
type
==QEvent::MouseTrackingChange ||
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.
Re: QWebView and right click reload
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.