Permalink
Browse files

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.
  • Loading branch information...
The-Compiler committed Aug 18, 2016
1 parent 25faa04 commit 0557fea79e50086fdcef6b51fa1da4eeda28000b
@@ -562,7 +562,7 @@ def _set_load_status(self, val):
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
@@ -1953,17 +1953,17 @@ def fake_key(self, keystring, global_=False):
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')
except objreg.RegistryUnavailableError:
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)
@@ -384,7 +384,7 @@ def click(self, click_target):
]
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 @@ def hover(self):
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)
@@ -201,8 +201,8 @@ def _key_press(self, key, count=1):
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 @@ def _connect_signals(self):
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)
@@ -696,7 +696,5 @@ def _connect_signals(self):
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)
@@ -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
@@ -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'

0 comments on commit 0557fea

Please sign in to comment.