Can PyQt QWebPage linkClicked signal detect which mouse button was used
我正在将此信号与处理单击的函数连接。例如:
1 2 3 4 | QtCore.QObject.connect(self.ui.webView, QtCore.SIGNAL("linkClicked (const QUrl&)"), self.navigate) def navigate(self, url): #do something |
我想让导航功能知道链接是用鼠标左键还是右键单击的。有点像
1 2 3 4 5 | def navigate(self, url, event): if event == Qt.LeftClick: #do something if event == Qt.MidClick: #do something else |
号
这有可能吗?
另一种方法是重新实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #!/usr/bin/env python #-*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui class myWindow(QtGui.QWidget): def __init__(self, parent=None): super(myWindow, self).__init__(parent) self.label = QtGui.QLabel(self) self.label.setText("Click Me") self.layout = QtGui.QHBoxLayout(self) self.layout.addWidget(self.label) def mousePressEvent(self, event): if event.buttons() == QtCore.Qt.LeftButton: self.label.setText("Left Mouse Click!") elif event.buttons() == QtCore.Qt.RightButton: self.label.setText("Right Mouse Click!") return super(myWindow, self).mousePressEvent(event) if __name__ =="__main__": import sys app = QtGui.QApplication(sys.argv) app.setApplicationName('myWindow') main = myWindow() main.resize(150, 150) main.show() sys.exit(app.exec_()) |
您应该子类
在您的插槽中,您可以简单地检查上一次按下按钮的值,并按您想要的方式处理它。
如果您查看这些文档,您可以在事件过滤器qmouseevent.button docs中处理有关在qmouseevent中按过哪些按钮的信息。