QWebEngineView不允许拖拽
时间: 2023-09-12 19:08:44 浏览: 71
在 QWebEngineView 中,拖放操作默认是被禁用的。如果你想启用拖放操作,可以使用以下代码:
```python
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QDragEnterEvent, QDropEvent
from PyQt5.QtWebEngineWidgets import QWebEngineView
class DragDropWebView(QWebEngineView):
def __init__(self, parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event: QDragEnterEvent):
if event.mimeData().hasUrls():
event.acceptProposedAction()
def dropEvent(self, event: QDropEvent):
for url in event.mimeData().urls():
self.load(url.toString())
```
这个例子中,我们创建了一个新的类 `DragDropWebView`,继承了 `QWebEngineView`。我们在构造函数中调用了 `setAcceptDrops(True)` 方法,启用了拖放操作。我们还重写了 `dragEnterEvent` 和 `dropEvent` 方法,分别处理拖入和释放事件。
在 `dragEnterEvent` 方法中,我们检查了拖入的数据是否包含 URLs,如果包含,我们调用 `event.acceptProposedAction()` 方法,表示接受此次拖拽操作。
在 `dropEvent` 方法中,我们遍历了所有拖拽事件中的 URLs,并使用 `load()` 方法加载了这些 URLs。
这样,我们就可以在 QWebEngineView 中启用拖放操作了。