Use QApplication.sendEvent instead of postEvent

From the QApplication.postEvent docs:
http://doc.qt.io/qt-5/qcoreapplication.html#postEvent

  The event must be allocated on the heap since the post event queue
  will take ownership of the event and delete it once it has been
  posted. It is not safe to access the event after it has been posted.

We can't reliably guarantee that from Python, so we need to use
sendEvent instead.
This commit is contained in:
Florian Bruhin 2016-08-18 21:36:43 +02:00
parent 25faa04196
commit 0557fea79e
7 changed files with 19 additions and 18 deletions

View File

@ -562,7 +562,7 @@ class AbstractTab(QWidget):
self._load_status = val
self.load_status_changed.emit(val.name)
def post_event(self, evt):
def send_event(self, evt):
"""Send the given event to the underlying widget."""
raise NotImplementedError

View File

@ -1953,8 +1953,8 @@ class CommandDispatcher:
window = QApplication.focusWindow()
if window is None:
raise cmdexc.CommandError("No focused window!")
QApplication.postEvent(window, press_event)
QApplication.postEvent(window, release_event)
QApplication.sendEvent(window, press_event)
QApplication.sendEvent(window, release_event)
else:
try:
tab = objreg.get('tab', scope='tab', tab='current')
@ -1962,8 +1962,8 @@ class CommandDispatcher:
raise cmdexc.CommandError("No focused webview!")
tab = self._current_widget()
tab.post_event(press_event)
tab.post_event(release_event)
tab.send_event(press_event)
tab.send_event(release_event)
@cmdutils.register(instance='command-dispatcher', scope='window',
debug=True)

View File

@ -384,7 +384,7 @@ class AbstractWebElement(collections.abc.MutableMapping):
]
for evt in events:
self._tab.post_event(evt)
self._tab.send_event(evt)
def after_click():
"""Move cursor to end and reset override_target after clicking."""
@ -398,4 +398,4 @@ class AbstractWebElement(collections.abc.MutableMapping):
pos = self._mouse_pos()
event = QMouseEvent(QEvent.MouseMove, pos, Qt.NoButton, Qt.NoButton,
Qt.NoModifier)
self._tab.post_event(event)
self._tab.send_event(event)

View File

@ -201,8 +201,8 @@ class WebEngineScroller(browsertab.AbstractScroller):
press_evt = QKeyEvent(QEvent.KeyPress, key, Qt.NoModifier, 0, 0, 0)
release_evt = QKeyEvent(QEvent.KeyRelease, key, Qt.NoModifier, 0, 0, 0)
for _ in range(count):
self._tab.post_event(press_evt)
self._tab.post_event(release_evt)
self._tab.send_event(press_evt)
self._tab.send_event(release_evt)
@pyqtSlot()
def _update_pos(self):
@ -523,8 +523,6 @@ class WebEngineTab(browsertab.AbstractTab):
except AttributeError:
log.stub('contentsSizeChanged, on Qt < 5.7')
def post_event(self, evt):
# If we get a segfault here, we might want to try sendEvent
# instead.
def send_event(self, evt):
recipient = self._widget.focusProxy()
QApplication.postEvent(recipient, evt)
QApplication.sendEvent(recipient, evt)

View File

@ -696,7 +696,5 @@ class WebKitTab(browsertab.AbstractTab):
frame.initialLayoutCompleted.connect(self._on_history_trigger)
page.link_clicked.connect(self._on_link_clicked)
def post_event(self, evt):
# If we get a segfault here, we might want to try sendEvent
# instead.
QApplication.postEvent(self._widget, evt)
def send_event(self, evt):
QApplication.sendEvent(self._widget, evt)

View File

@ -72,6 +72,11 @@ Feature: Scrolling
And I run :scroll left
Then the page should not be scrolled
# causes segfault with postEvent instead of sendEvent
Scenario: Scrolling down with count 10
When I run :scroll down with count 10
Then no crash should happen
Scenario: Scrolling with page down
When I run :scroll page-down
Then the page should be scrolled vertically

View File

@ -39,7 +39,7 @@ class EventObject(QObject):
def test_log_events(qapp, caplog):
obj = EventObject()
qapp.postEvent(obj, QEvent(QEvent.User))
qapp.sendEvent(obj, QEvent(QEvent.User))
qapp.processEvents()
assert len(caplog.records) == 1
assert caplog.records[0].msg == 'Event in test_debug.EventObject: User'