Skip to content

Commit

Permalink
Fix our patched QMessagBox not working on older PyQt5
Browse files Browse the repository at this point in the history
  • Loading branch information
stargateaudio committed Nov 13, 2022
1 parent 5eb6417 commit 6599507
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/sgui/sgqt.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,16 @@ def exec(self, block=True, center=True):

orig_QMessageBox = QMessageBox

# Required to support older versions of PyQt5. Only added the ones that
# are used. Previously the entire StandardButton enum was iterated, but
# that is not possible on the sip.enumtype old versions used
_QMESSAGEBOX_STANDARDBUTTON_NAMES = {
QMessageBox.StandardButton.Ok: "OK",
QMessageBox.StandardButton.Cancel: "Cancel",
QMessageBox.StandardButton.Yes: "Yes",
QMessageBox.StandardButton.No: "No",
}

class _QMessageBox:
StandardButton = orig_QMessageBox.StandardButton

Expand All @@ -498,24 +508,21 @@ def close(_answer):
else:
answer.append(_answer)
dialog.close()
def add_button(_enum):
def add_button(_int, name):
# Needs to be a separate function so that the value of
# _enum is in the stack frame
button = QPushButton(_enum.name)
button.pressed.connect(lambda: close(_enum))
button = QPushButton(name)
button.pressed.connect(lambda: close(_int))
buttons_layout.addWidget(button)
dialog = QDialog()
layout = QVBoxLayout(dialog)
#layout.addWidget(QLabel(title))
layout.addWidget(QLabel(message))
buttons_layout = QHBoxLayout()
layout.addLayout(buttons_layout)
for _enum in QMessageBox.StandardButton:
if _enum not in (
QMessageBox.StandardButton.ButtonMask,
QMessageBox.StandardButton.FlagMask,
) and buttons & _enum.value:
add_button(_enum)
for _int, name in _QMESSAGEBOX_STANDARDBUTTON_NAMES.items():
if buttons & _int:
add_button(_int, name)
dialog.exec(block=not callbacks)
if not callbacks:
answer = answer.pop()
Expand Down

0 comments on commit 6599507

Please sign in to comment.