[webkit-qt] Fwd: Custom Context Menu Events

Simon Hausmann simon.hausmann at digia.com
Fri Mar 1 00:32:44 PST 2013

On Thursday, February 28, 2013 08:14:23 PM Mitchell Verter wrote:
> Hi all,
> I am fairly new to QT and the QT-Webkit, so I was hoping someone could
> guide me through this:
> ====================
> I'm trying to think through the problem with the context menus.
> The issue is that it gives several different menus, depending on the
> context.
> A.  Page Contex
> .. i.  Forward
> .. ii. Reload
> ..  iii. Back
> B.  Image Context
> .. i. Copy Image
> .. ii.  Save Image
> .. iii.  Copy Image Link (?)
> The issue is that we want to limit Image context (B) to do only (B.ii)
> I am guessing that we would need to do something with:
> ============================================
>  bool <http://qt-project.org/doc/qt-4.7/qml-bool.html>QWebPage
> swallowContextMenuEvent(QContextMenuEvent<http://qt-project.org/doc/qt-4.7/q
> contextmenuevent.html> *
> event)
> Filters the context menu event, *event*, through handlers for scrollbars
> and custom event handlers in the web page. Returns true if the event was
> handled; otherwise false.
> A web page may swallow a context menu event through a custom event handler,
> allowing for context menus to be implemented in HTML/JavaScript. This is
> used by Google Maps <http://maps.google.com/>, for example.
> Look up this member
> <http://qt.gitorious.org/qt/qt/blobs/4.7/src/3rdparty/webkit/WebKit/qt/Api/q
> webpage.h#line256>in the source code.
> =================================================
> How would we retrict it to context (B)?
> Someone told me this previously:
> [14:52]  so how do i constrain this to only affect the image context menu
> [14:54]  QWebPage::hitTestContent(const QPoint &Â pos) and
> !QWebHitTestResult::imageUrl().isEmpty()

Yes, roughly.

swallowContextMenuEvent is about allowing web content to provide their own 
context menu. If you don't want that, then you need to re-implement 

If you want to customize the appearance of the existing context menu (provided 
by WebKit), then I suggest to re-implement contextMenuEvent() and roll up your 
own, based on the information that hitTestContent() provides you with, i.e. if 
the mouse is under an image, a link, etc.

You can re-use existing QAction objects, just fetch it from the QWebPage and 
add it to your QMenu.


More information about the webkit-qt mailing list