+ Reply to Thread
Results 1 to 8 of 8

Thread: QWebView and right click reload

  1. #1
    Join Date
    Dec 2010
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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:

    Qt Code:
    1. MyApplication::MyApplication(int argc, char *argv[]) :
    2. QApplication(argc, argv)
    3. {
    4. installEventFilter(this);
    5. }
    6.  
    7. bool MyApplication::eventFilter(QObject *obj, QEvent *e)
    8. {
    9. if ( ( e->type() == QEvent::MouseButtonPress )||
    10. ( e->type() == QEvent::MouseButtonRelease )||
    11. ( e->type() == QEvent::MouseButtonDblClick ) )
    12. {
    13. return true;
    14. //QMouseEvent* m = static_cast<QMouseEvent *>(e);
    15. //int i = m->button();
    16. //if ( i == Qt::RightButton)
    17. //{
    18. // return true;
    19. //}
    20. }
    21. else if ( e->type() == QEvent::KeyPress )
    22. {
    23. QKeyEvent* k = static_cast<QKeyEvent *>(e);
    24. if(k->key() == Qt::Key_F4)
    25. {
    26. quit();
    27. return true;
    28. }
    29. }
    30. return QApplication::eventFilter(obj, e);
    31. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Mar 2010
    Posts
    289
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWebView and right click reload

    What about using your own context menu? That's what I personally do:
    Qt Code:
    1. myBrowser->setContextMenuPolicy(Qt::CustomContextMenu);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to agarny for this useful post:

    sky (16th March 2011)

  4. #3
    Join Date
    Dec 2010
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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.
    Last edited by sky; 16th March 2011 at 05:09.

  5. #4
    Join Date
    Mar 2010
    Posts
    289
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWebView and right click reload

    Quote Originally Posted by sky View Post
    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 View Post
    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?

  6. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 346 Times in 332 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QWebView and right click reload

    Quote Originally Posted by sky View Post
    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.

    Qt Code:
    1. mWebView->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Dec 2010
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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

    Qt Code:
    1. Browser::Browser(QWidget *parent)
    2. : QWebView(parent), m_page(this), m_pXMLHttp(NULL)
    3. {
    4.  
    5. installEventFilter(this);
    6.  
    7. setPage(&m_page);
    8. m_pFrame = page()->mainFrame();
    9.  
    10. m_page.installEventFilter(this);
    11. m_pFrame->installEventFilter(this);
    12. }
    13.  
    14. bool Browser::eventFilter(QObject *obj, QEvent *event)
    15. {
    16. if ( (event->type() == QEvent::MouseButtonPress )||
    17. (event->type() == QEvent::MouseButtonRelease)||
    18. ( event->type() == QEvent::MouseButtonDblClick ) )
    19. {
    20. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
    21. if (mouseEvent->button() == Qt::RightButton)
    22. {
    23. mouseEvent->accept();
    24. return true;
    25. }
    26. }
    27. //return QWebView::eventFilter(obj, event);
    28. return false;
    29. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Aug 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

  9. #8
    Join Date
    May 2013
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:

    Qt Code:
    1. bool MyObject::eventFilter( QObject *watched, QEvent *event )
    2. {
    3. if( NULL != event )
    4. {
    5. QEvent::Type type = event->type();
    6. if( ! m_bMouseEnabled &&
    7. ( type==QEvent::MouseButtonDblClick ||
    8. type==QEvent::MouseButtonPress ||
    9. type==QEvent::MouseButtonRelease ||
    10. type==QEvent::MouseMove ||
    11. type==QEvent::MouseTrackingChange ||
    12. type==QEvent::ContextMenu ||
    13. type==QEvent::GraphicsSceneContextMenu )
    14. )
    15. {
    16. return true;
    17. }
    18. }
    19. return false;
    20. }
    21.  
    22. void WebKitView::initialize()
    23. {
    24. m_webView->installEventFilter( m_myObj );
    25. }
    To copy to clipboard, switch view to plain text mode 

    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.

  10. The following user says thank you to garibaldi76 for this useful post:

    stevensreese (24th May 2013)

+ Reply to Thread

Similar Threads

  1. how do you reload a listView?
    By implor in forum Newbie
    Replies: 5
    Last Post: 16th May 2010, 22:21
  2. Reload a Qt Plugin
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 30th December 2009, 15:36
  3. Replies: 2
    Last Post: 11th January 2009, 23:24
  4. Reload a QTableWidget
    By SailinShoes in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2008, 11:40
  5. How to reload widget plugins?
    By victorng in forum Qt Programming
    Replies: 2
    Last Post: 1st March 2006, 23:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts