I have a dialog window that's called from the QMainWindow as:
def addTransfer(self):
try:
res = Transfert(self.db, self.supplierid, parent=self)
res.show()
res.exec()
except DataError as err:
QMessageBox.warning(self, err.source, err,message, QMessageBox.OK)
The dialog widget is like:
class Transfer(QDialog):
def __init__(self, supplierId, parent=None)
super().__init__(parent)
........................
@pyqtSlot()
def save(def):
try:
qry = QSqlQuery(self.db)
................
................
if qry.lastError().type() != 0:
raise DataError("save", qry.lastError().text())
except DataError as err:
print(err.source, err.message)
raise Dataerror(err.source, err.message)
, Whenever a Data error is threw, it is caught in the try block but it will not be caught at the try block of the calling function. It just prints the traceback etc to the screen. The same behavior happens when leaving the error unhandle at the dialog widget, or when trying a similar process at other slots. I´ve been looking over to find that PyQt does not propagate errors to the caller when they happen at a slot level, but there are workarounds that could be implemented such as described at Preventing PyQt to silence exceptions occurring in slots but unfortunally - for me - I can not understand how to make it work. Any help will be most appreciated
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QDialog, QLineEdit,
QPushButton, QMessageBox, QHBoxLayout, QLabel)
from ext.APM import DataError
Minimal Reproducible Example:
class MainTest(QMainWindow):
def __init__(self):
super().__init__()
self.setUI()
def setUI(self):
self.setWindowTitle('Test')
self.setMinimumSize(500, 500)
lblTest = QLabel("Test)")
pushOpenDialog = QPushButton("Open")
pushOpenDialog.setMinimumSize(50, 50)
pushOpenDialog.clicked.connect(self.openDialog)
self.setCentralWidget(pushOpenDialog)
def openDialog(self):
try:
diag = DialogTest()
diag.show()
diag.exec()
except DataError as err:
QMessageBox.critical(self, err.source, err.message)
class DialogTest(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setUi()
def setUi(self):
self.setWindowTitle('Dialog')
self.setMinimumSize(500, 500)
lblTest = QLabel("Test:")
pushTest = QPushButton("Open")
pushTest.clicked.connect(self.test)
hLayout = QHBoxLayout()
hLayout.addWidget(lblTest)
hLayout.addWidget(pushTest)
self.setLayout(hLayout)
def test(self):
try:
1/0
except ZeroDivisionError as err:
raise DataError("test", err.args)
def main():
app = QApplication(sys.argv)
window = MainTest()
window.show()
app.exec_()
if __name__ == '__main__':
main()