如何在离线绘图上自定义或禁用右键菜单?
How to customise or disable right click menu on offline plotly plot?
我正在以与此代码类似的方式创建离线绘图:
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow
import plotly.graph_objects as go
import plotly
import numpy as np
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# some example data
x = np.arange(1000)
y = x**2
# create the plotly figure
fig = go.Figure(go.Scatter(x=x, y=y))
# we create html code of the figure
html = '<html><body>'
html += plotly.offline.plot(fig, output_type='div', include_plotlyjs='cdn')
html += '</body></html>'
# we create an instance of QWebEngineView and set the html code
plot_widget = QWebEngineView()
plot_widget.setHtml(html)
# set the QWebEngineView instance as main widget
self.setCentralWidget(plot_widget)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
用鼠标左键单击我可以访问导航(比如缩放平移),但是右键单击目前对我来说完全没用,所以我想把我自己的操作放在右键菜单中,或者如果至少禁用它是不可能的。有没有办法巧妙地做到这一点?
您必须覆盖 contextMenuEvent 方法并添加所需的 QActions 和 QMenus。
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWebEngineWidgets import QWebEngineView
import plotly.graph_objects as go
import plotly
import numpy as np
class WebEngineView(QWebEngineView):
def contextMenuEvent(self, event):
menu = self.page().createStandardContextMenu()
menu.addSeparator()
custom_action = menu.addAction("Custom Action")
custom_action.triggered.connect(self.handle_custom_action)
menu.exec_(event.globalPos())
def handle_custom_action(self):
print("custom_action")
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
x = np.arange(1000)
y = x ** 2
fig = go.Figure(go.Scatter(x=x, y=y))
html = "".join(
[
"<html><body>",
plotly.offline.plot(fig, output_type="div", include_plotlyjs="cdn"),
"</body></html>",
]
)
plot_widget = WebEngineView()
plot_widget.setHtml(html)
self.setCentralWidget(plot_widget)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
如果您想禁用上下文菜单,则必须将 Qt.NoContextMenu 设置为 contextMenuPolicy:
plot_widget.setContextMenuPolicy(Qt.NoContextMenu)
我正在以与此代码类似的方式创建离线绘图:
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow
import plotly.graph_objects as go
import plotly
import numpy as np
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# some example data
x = np.arange(1000)
y = x**2
# create the plotly figure
fig = go.Figure(go.Scatter(x=x, y=y))
# we create html code of the figure
html = '<html><body>'
html += plotly.offline.plot(fig, output_type='div', include_plotlyjs='cdn')
html += '</body></html>'
# we create an instance of QWebEngineView and set the html code
plot_widget = QWebEngineView()
plot_widget.setHtml(html)
# set the QWebEngineView instance as main widget
self.setCentralWidget(plot_widget)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
用鼠标左键单击我可以访问导航(比如缩放平移),但是右键单击目前对我来说完全没用,所以我想把我自己的操作放在右键菜单中,或者如果至少禁用它是不可能的。有没有办法巧妙地做到这一点?
您必须覆盖 contextMenuEvent 方法并添加所需的 QActions 和 QMenus。
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWebEngineWidgets import QWebEngineView
import plotly.graph_objects as go
import plotly
import numpy as np
class WebEngineView(QWebEngineView):
def contextMenuEvent(self, event):
menu = self.page().createStandardContextMenu()
menu.addSeparator()
custom_action = menu.addAction("Custom Action")
custom_action.triggered.connect(self.handle_custom_action)
menu.exec_(event.globalPos())
def handle_custom_action(self):
print("custom_action")
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
x = np.arange(1000)
y = x ** 2
fig = go.Figure(go.Scatter(x=x, y=y))
html = "".join(
[
"<html><body>",
plotly.offline.plot(fig, output_type="div", include_plotlyjs="cdn"),
"</body></html>",
]
)
plot_widget = WebEngineView()
plot_widget.setHtml(html)
self.setCentralWidget(plot_widget)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
如果您想禁用上下文菜单,则必须将 Qt.NoContextMenu 设置为 contextMenuPolicy:
plot_widget.setContextMenuPolicy(Qt.NoContextMenu)