You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Sunday, March 8th 2009, 8:59pm

QWebView selected text context menu

hi, i would like to implement a custom context menu for when a selection of text is right-clicked in qwebview. Can someone point me to the docs about how I would do this? By default the menu shown has just a one entry: "Copy".

2

Monday, March 9th 2009, 1:39pm

See demos\browser from qt library.
From QWebView docs:

Source code

1
If you require a custom context menu, you can implement it by reimplementing contextMenuEvent() and populating your QMenu with the actions obtained from pageAction(). More functionality such as reloading the view, copying selected text to the clipboard, or pasting into the view, is also encapsulated within the QAction objects returned by pageAction(). These actions can be programmatically triggered using triggerPageAction(). Alternatively, the actions can be added to a toolbar or a menu directly. QWebView maintains the state of the returned actions but allows modification of action properties such as text or icon.

Cut from browser demo:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void WebView::contextMenuEvent(QContextMenuEvent *event)
{
    QWebHitTestResult r = page()->mainFrame()->hitTestContent(event->pos());
    if (!r.linkUrl().isEmpty()) {
        QMenu menu(this);
        menu.addAction(pageAction(QWebPage::OpenLinkInNewWindow));
        menu.addAction(tr("Open in New Tab"), this, SLOT(openLinkInNewTab()));
        menu.addSeparator();
        menu.addAction(pageAction(QWebPage::DownloadLinkToDisk));
        // Add link to bookmarks...
        menu.addSeparator();
        menu.addAction(pageAction(QWebPage::CopyLinkToClipboard));
        if (page()->settings()->testAttribute(QWebSettings::DeveloperExtrasEnabled))
            menu.addAction(pageAction(QWebPage::InspectElement));
        menu.exec(mapToGlobal(event->pos()));
        return;
    }
    QWebView::contextMenuEvent(event);
}
Fighting fire with fire.
Three can keep a secret if two of them are dead.

3

Monday, March 9th 2009, 3:30pm

I want to just override the context menu when you right click on a highlighted piece of text, is it possible to get that fine-grained?
edit: whoops, i'm stupid. thanks!