Results 1 to 7 of 7

Thread: How to disable right click on web page

  1. #1
    Join Date
    Feb 2016
    Posts
    15
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default How to disable right click on web page

    Hi all

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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to disable right click on web page

    You could try with an event filter, see QObject::eventFilter().

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    15
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to disable right click on web page

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to disable right click on web page

    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,
    _

  5. #5
    Join Date
    Feb 2016
    Posts
    15
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: How to disable right click on web page

    Qt Code:
    1. webengview->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. bool My_class::eventFilter( QObject * obj, QEvent * event )
    2. {
    3. if (event->type() == QEvent::MouseButtonPress) {
    4. QMouseEvent* m = (QMouseEvent*) event;
    5.  
    6. m->ignore();
    7. return true;
    8. }
    9.  
    10. bool val= QObject::eventFilter(obj, event);
    11.  
    12. QWebEngineView * menu = dynamic_cast<QWebEngineView*>(obj);
    13.  
    14. if(menu && event->type() == QEvent::MouseButtonPress)
    15. {
    16. QMouseEvent * ev = dynamic_cast<QMouseEvent*>(event);
    17. if(ev)
    18. {
    19. if(ev->button() == Qt::RightButton)
    20. {
    21. ev->ignore();
    22. return false; // yes we filter the event
    23. }
    24.  
    25. }
    26. }
    27. return val;
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. webengview->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. bool My_class::eventFilter( QObject * obj, QEvent * event )
    2. {
    3. bool val= QObject::eventFilter(obj, event);
    4.  
    5. QWebEngineView * menu = dynamic_cast<QWebEngineView*>(obj);
    6.  
    7. if(menu && event->type() == QEvent::MouseButtonPress)
    8. {
    9. QMouseEvent * ev = dynamic_cast<QMouseEvent*>(event);
    10. if(ev)
    11. {
    12. if(ev->button() == Qt::RightButton)
    13. {
    14. ev->ignore();
    15. return false; // yes we filter the event
    16. }
    17.  
    18. }
    19. }
    20. return val;
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 8th March 2016 at 16:55. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to disable right click on web page

    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,
    _

  7. #7

    Default Re: How to disable right click on web page

    QWebEngineView is derived from QWidget, so you can use
    Qt Code:
    1. setContextMenuPolicy( Qt::NoContextMenu )
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. setContextMenuPolicy( Qt::PreventContextMenu )
    To copy to clipboard, switch view to plain text mode 
    to disable the right clicks. Both behave slightly differently and pass the mouse clicks in different ways to the underlying WebPage.

Similar Threads

  1. Disable right mouse click on QAction
    By stefan in forum Newbie
    Replies: 13
    Last Post: 15th May 2013, 23:04
  2. Replies: 0
    Last Post: 8th November 2011, 12:16
  3. Disable button click
    By "BumbleBee" in forum Newbie
    Replies: 19
    Last Post: 30th March 2011, 14:16
  4. How to disable mouse click over a QSplashScreen
    By graciano in forum Qt Programming
    Replies: 2
    Last Post: 8th November 2009, 17:02
  5. Replies: 3
    Last Post: 25th August 2009, 23:35

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.