Skip to content

Commit

Permalink
Use exec instead of exec_ where possible for Qt (#1208)
Browse files Browse the repository at this point in the history
This uses `hasattr` to test whether `exec` is available, which should be all Qt backends except PySide2.

Fixes #1179
  • Loading branch information
corranwebster authored Mar 9, 2023
1 parent 4ff09bf commit 6133521
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 18 deletions.
5 changes: 4 additions & 1 deletion pyface/ui/qt4/code_editor/code_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,4 +839,7 @@ def set_trace():

window.resize(640, 640)
window.show()
sys.exit(app.exec_())
if hasattr(app, "exec"):
sys.exit(app.exec())
else:
sys.exit(app.exec_())
9 changes: 5 additions & 4 deletions pyface/ui/qt4/confirmation_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ class ConfirmationDialog(MConfirmationDialog, Dialog):

# If we create custom buttons with the various roles, then we need to
# keep track of the buttons so we can see what the user clicked. It's
# not correct nor sufficient to check the return result from QMessageBox.exec_().
# (As of Qt 4.5.1, even clicking a button with the YesRole would lead to
# exec_() returning QDialog.DialogCode.Rejected.
# not correct nor sufficient to check the return result from QMessageBox.exec().
_button_result_map = Dict()

# ------------------------------------------------------------------------
Expand Down Expand Up @@ -127,7 +125,10 @@ def _create_control(self, parent):

def _show_modal(self):
self.control.setWindowModality(QtCore.Qt.WindowModality.ApplicationModal)
retval = self.control.exec_()
if hasattr(self.control, "exec"):
retval = self.control.exec()
else:
retval = self.control.exec_()
if self.control is None:
# dialog window closed
if self.cancel:
Expand Down
25 changes: 20 additions & 5 deletions pyface/ui/qt4/console/console_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,12 @@ def print_(self, printer=None):
"""
if not printer:
printer = QtGui.QPrinter()
if QtGui.QPrintDialog(printer).exec_() != QtGui.QDialog.DialogCode.Accepted:
dialog = QtGui.QPrintDialog(printer)
if hasattr(dialog, "exec"):
result = dialog.exec()
else:
result = dialog.exec_()
if result != QtGui.QDialog.DialogCode.Accepted:
return
self._control.print_(printer)

Expand All @@ -616,7 +621,11 @@ def export(self, parent=None):
root, ext = os.path.splitext(self._filename)
if ext.lower() in (".xml", ".xhtml"):
dialog.selectNameFilter(filters[-1])
if dialog.exec_():
if hasattr(dialog, "exec"):
result = dialog.exec()
else:
result = dialog.exec_()
if result:
filename = str(dialog.selectedFiles()[0])
self._filename = filename
choice = str(dialog.selectedNameFilter())
Expand Down Expand Up @@ -688,8 +697,11 @@ def export_html(self, filename):
layout.addWidget(checkbox)
widget.setLayout(layout)
widget.show()
reply = box.exec_()
inline = reply == 0
if hasattr(box, "exec"):
reply = box.exec()
else:
reply = box.exec_()
inline = (reply == 0)
if checkbox.checkState():
# don't ask anymore, always use this choice
if inline:
Expand Down Expand Up @@ -2029,4 +2041,7 @@ def _custom_context_menu_requested(self, pos):
""" Shows a context menu at the given QPoint (in widget coordinates).
"""
menu = self._context_menu_make(pos)
menu.exec_(self._control.mapToGlobal(pos))
if hasattr(menu, "exec"):
menu.exec(self._control.mapToGlobal(pos))
else:
menu.exec_(self._control.mapToGlobal(pos))
5 changes: 4 additions & 1 deletion pyface/ui/qt4/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ def _show_modal(self):
dialog.windowFlags() & ~QtCore.Qt.WindowType.WindowContextHelpButtonHint
)

retval = dialog.exec_()
if hasattr(self.control, "exec"):
retval = self.control.exec()
else:
retval = self.control.exec_()
return _RESULT_MAP[retval]

# -------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion pyface/ui/qt4/single_choice_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ def _create_contents(self, parent):

def _show_modal(self):
self.control.setWindowModality(QtCore.Qt.WindowModality.ApplicationModal)
retval = self.control.exec_()
if hasattr(self.control, 'exec'):
retval = self.control.exec()
else:
retval = self.control.exec_()
if self.control is None:
# dialog window closed, treat as Cancel, nullify choice
retval = CANCEL
Expand Down
10 changes: 8 additions & 2 deletions pyface/ui/qt4/tasks/split_editor_area_pane.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,10 @@ def contextMenuEvent(self, event):
menu = self.editor_area.get_context_menu(pos=global_pos)
if menu is not None:
qmenu = menu.create_menu(self)
qmenu.exec_(global_pos)
if hasattr(qmenu, 'exec'):
qmenu.exec(global_pos)
else:
qmenu.exec_(global_pos)

def dragEnterEvent(self, event):
""" Re-implemented to highlight the tabwidget on drag enter
Expand Down Expand Up @@ -1026,7 +1029,10 @@ def mouseMoveEvent(self, event):
drag.setPixmap(self.drag_obj.get_pixmap())
drag.setHotSpot(self.drag_obj.get_hotspot())
drag.setMimeData(mimedata)
drag.exec_()
if hasattr(drag, 'exec'):
drag.exec()
else:
drag.exec_()
self.drag_obj = None # deactivate the drag_obj again
return
return super().mouseMoveEvent(event)
Expand Down
10 changes: 8 additions & 2 deletions pyface/ui/qt4/util/event_loop_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ def do_timeout():
timeout_timer.start()
condition_timer.start()
try:
self.qt_app.exec_()
if hasattr(self.qt_app, 'exec'):
self.qt_app.exec()
else:
self.qt_app.exec_()
if not condition_result:
if condition_result is None:
status = "without evaluating condition"
Expand Down Expand Up @@ -203,7 +206,10 @@ def delete_widget(self, widget, timeout=1.0):
widget.destroyed.connect(self.qt_app.quit)
yield
timer.start()
self.qt_app.exec_()
if hasattr(self.qt_app, 'exec'):
self.qt_app.exec()
else:
self.qt_app.exec_()
if not timer.isActive():
# We exited the event loop on timeout
raise ConditionTimeoutError(
Expand Down
5 changes: 4 additions & 1 deletion pyface/ui/qt4/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ def delete_widget(widget, timeout=1.0):
yield

timer.start()
app.exec_()
if hasattr(app, 'exec'):
app.exec()
else:
app.exec_()

if not timer.isActive():
# We exited the event loop on timeout.
Expand Down
5 changes: 4 additions & 1 deletion pyface/util/guisupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ def start_event_loop_qt4(app=None):
app = get_app_qt4([""])
if not is_event_loop_running_qt4(app):
app._in_event_loop = True
app.exec_()
if hasattr(app, 'exec'):
app.exec()
else:
app.exec_()
app._in_event_loop = False
else:
app._in_event_loop = True
Expand Down

0 comments on commit 6133521

Please sign in to comment.