【问题标题】:Add items to the standard QWebView context menu [closed]将项目添加到标准 QWebView 上下文菜单 [关闭]
【发布时间】:2016-08-15 07:44:53
【问题描述】:

QWebView 的实现有一个标准的上下文菜单。我想更改它并创建自己的 - 或将“在新选项卡中打开”添加到标准上下文菜单,然后将其连接到我的应用程序。怎么做?

【问题讨论】:

  • 你有没有尝试过?你能给我们看一些代码吗?
  • 你能告诉我之后我能做什么,...setContextMenuPolicy(0)(关闭)。如何创建新的上下文菜单?

标签: python qt4 pyqt4 contextmenu qwebview


【解决方案1】:

你可以重新实现QWebView.contextMenuEvent:

class WebView(QtWebKit.QWebView):
    def __init__(self, parent=None):
        super(WebView, self).__init__(parent)
        self.newTabAction = QtGui.QAction('Open in new tab', self)
        self.newTabAction.triggered.connect(self.createNewTab)

    def createNewTab(self):
        url = self.newTabAction.data()
        print('create new tab:', url.toString())

    def contextMenuEvent(self, event):
        menu = self.page().createStandardContextMenu()
        hit = self.page().currentFrame().hitTestContent(event.pos())
        url = hit.linkUrl()
        if url.isValid():
            self.newTabAction.setData(url)
            menu.addAction(self.newTabAction)
        menu.exec_(event.globalPos())

如果您不想使用标准上下文菜单,只需使用QtGui.QMenu() 创建您自己的。

【讨论】:

  • 谢谢,它适用于所有网页。但我只想悬停在链接上。
  • 谢谢!我能够成功地使用它来实现图像的上下文菜单(通过将linkUrl 替换为imageUrl)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-08
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
  • 2013-05-11
  • 2022-11-11
相关资源
最近更新 更多