PyQT program crashes on Mac (not on Windows) when clicking on QComboBox
-
I am using pyqt in python for a program with a QDialog and a QMainWindow. When the program starts, the QDialog is shown. Here the user can choose a file type with a QComboBox and there is a button to open the directory and choose a file. Once this is done, the QDialog is closed and the QMainWindow is shown. On the QMainWindow, there is an option to go back to the QDialog and choose another file.
All these things work perfectly, but a problem comes when I go back to the QDialog from the QMainWindow and I click on the QComboBox. The program is closed with an error of EXC_BAD_ACCESS and KERN_INVALID_ADDRESS. This just happens when I click on the QComboBox, not on any other widget in the screen. Also, this just happend when using Mac, with Windows I do not have any problem.
The code I use to open the QDialog isdef request_user_input_database_and_file(self): try: app = QApplication(sys.argv) mainWindow = MyMainImport(self) mainWindow.show() if QApplication.instance(): app.exec_() if self.continue_value == False: sys.exit(1) self.after_input() except: sys.exit(1)
The class that is referenced is
class MyMainImport(QDialog): def __init__(self, palms): super().__init__() self.initUI(palms=palms) # Get the desktop widget desktop = QApplication.desktop() # Get the screen height screen_height = desktop.screenGeometry().height() # Calculate the new height as 80% of the screen height new_height = int(screen_height * 0.8) self.setWindowIcon(QtGui.QIcon(str(ICON_PATH))) self.resize(850, new_height) # Set the window size (width, height) self.move(QDesktopWidget().availableGeometry().center() - self.rect().center()) # Set the starting position of the window (x, y) # Set the fixed size of the window self.setFixedSize(850, new_height) def initUI(self, palms): # central widget self.widget = ImportData() central_widget = QWidget() # create layout and add components self.lay = QVBoxLayout(central_widget) self.lay.addWidget(self.button) self.setLayout(self.lay) self.lay.addWidget(self.widget)
And the class with the combo box is
class ImportData(QWidget): def __init__(self, multiple=False): super().__init__() # Create the main vertical layout self.main_layout = QtWidgets.QVBoxLayout(self) # Create the scroll area scroll_area = QtWidgets.QScrollArea() scroll_area.setWidgetResizable(True) scroll_area.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) # Create the widget to hold the frames scroll_contents = QtWidgets.QWidget() self.layout = QFormLayout(scroll_contents) # Set the scroll area contents scroll_area.setWidget(scroll_contents) self.main_layout.addWidget(scroll_area) # Create the main selector layout selector_layout = QHBoxLayout() # Create and add the components for the first line selector_left_button = QPushButton("<") selector_left_button.setFixedHeight(20) selector_layout.addWidget(selector_left_button) self.currentSelector = 1 self.selector_label = QLabel("File " + str(self.currentSelector)) self.selector_label.setFixedHeight(20) selector_layout.addWidget(self.selector_label) selector_right_button = QPushButton(">") selector_right_button.setFixedHeight(20) selector_layout.addWidget(selector_right_button) self.file_type_combo = QComboBox() self.file_type_combo.addItem("csv") self.file_type_combo.addItem("ecg") self.file_type_combo.addItem("edf") self.file_type_combo.addItem("dat") self.file_type_combo.addItem("fit") self.file_type_combo.addItem("hea") self.file_type_combo.addItem("mat") self.file_type_combo.addItem("txt") self.file_type_combo.addItem("Other delimiter") self.file_type_combo.setFixedHeight(20) selector_layout.addWidget(self.file_type_combo) self.file_type_combo.currentTextChanged.connect(self.resetPath)
I made sure that each QDialog and QMainWindow is closed before opening the new one. And I know from debugging that a new QComboBox is properly created when calling the function request_user_input_database_and_file.
Does anyone have an idea of what can be the problem? I would really appreciate any help. Thank you -
@guillermollopis said in PyQT program crashes on Mac (not on Windows) when clicking on QComboBox:
def request_user_input_database_and_file(self):
try: app = QApplication(sys.argv)
Why do you create a new QApplication instance here? There should be only one QApplication instance.
It is also not clear how your main window and this dialog interact. Usually you instantiate a dialog from main window. -
@jsulm Thank you for your answer. I don't know why I wrote that line, but if I delete it I get the same error anyway. The main window is the class that contains the function request_user_input_database_and_file. When initialized, this function is called to open the QDialog. When the QDialog is closed, the function after_input() is called, where the QMainWindow is shown.
Maybe it's also relevant to add the crash report:
Crashed Thread: 0 Dispatch queue: com.apple.main-thread Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000028 Exception Codes: 0x0000000000000001, 0x0000000000000028 Exception Note: EXC_CORPSE_NOTIFY Termination Reason: Namespace SIGNAL, Code 11 Segmentation fault: 11 Terminating Process: exc handler [1820] VM Region Info: 0x28 is not in any region. Bytes before following region: 4311142360 REGION TYPE START - END [ VSIZE] PRT/MAX SHRMOD REGION DETAIL UNUSED SPACE AT START ---> __TEXT 100f6d000-100f79000 [ 48K] r-x/r-x SM=COW ...acOS/EZcardio Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0 libqcocoa.dylib 0x1168c0c54 QCocoaWindow::view() const + 4 1 libqcocoa.dylib 0x1168f072f QCocoaInputContext::reset() + 79 2 QtWidgets 0x10ef5959f QComboBox::showPopup() + 3071 3 QtWidgets.abi3.so 0x10e9d7345 sipQComboBox::showPopup() + 101 4 QtWidgets 0x10ef5a91b QComboBoxPrivate::showPopupFromMouseEvent(QMouseEvent*) + 619 5 QtWidgets.abi3.so 0x10e9d8079 sipQComboBox::mousePressEvent(QMouseEvent*) + 121 6 QtWidgets 0x10ee8fde8 QWidget::event(QEvent*) + 424 7 QtWidgets 0x10ef5a634 QComboBox::event(QEvent*) + 372 8 QtWidgets.abi3.so 0x10e9d783f sipQComboBox::event(QEvent*) + 191 9 QtWidgets 0x10ee559ea QApplicationPrivate::notify_helper(QObject*, QEvent*) + 266 10 QtWidgets 0x10ee58755 QApplication::notify(QObject*, QEvent*) + 6965 11 QtWidgets.abi3.so 0x10e9f2e06 sipQApplication::notify(QObject*, QEvent*) + 230 12 QtCore 0x10d6a9a34 QCoreApplication::notifyInternal2(QObject*, QEvent*) + 212 13 QtWidgets 0x10ee56310 QApplicationPrivate::sendMouseEvent(QWidget*, QMouseEvent*, QWidget*, QWidget*, QWidget**, QPointer<QWidget>&, bool, bool) + 896 14 QtWidgets 0x10eeaf022 QWidgetWindow::handleMouseEvent(QMouseEvent*) + 3266 15 QtWidgets 0x10eead6e9 QWidgetWindow::event(QEvent*) + 233 16 QtWidgets 0x10ee559ea QApplicationPrivate::notify_helper(QObject*, QEvent*) + 266 17 QtWidgets 0x10ee56e11 QApplication::notify(QObject*, QEvent*) + 497 18 QtWidgets.abi3.so 0x10e9f2e06 sipQApplication::notify(QObject*, QEvent*) + 230 19 QtCore 0x10d6a9a34 QCoreApplication::notifyInternal2(QObject*, QEvent*) + 212 20 QtGui 0x10e244e2e QGuiApplicationPrivate::processMouseEvent(QWindowSystemInterfacePrivate::MouseEvent*) + 3534 21 QtGui 0x10e229d7b QWindowSystemInterface::sendWindowSystemEvents(QFlags<QEventLoop::ProcessEventsFlag>) + 219 22 libqcocoa.dylib 0x1168d9260 QCocoaEventDispatcherPrivate::processPostedEvents() + 320 23 libqcocoa.dylib 0x1168d99c8 QCocoaEventDispatcherPrivate::postedEventsSourceCallback(void*) + 40 24 CoreFoundation 0x7ff81562cc08 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 25 CoreFoundation 0x7ff81562cb70 __CFRunLoopDoSource0 + 180 26 CoreFoundation 0x7ff81562c8e3 __CFRunLoopDoSources0 + 242 27 CoreFoundation 0x7ff81562b2ff __CFRunLoopRun + 897 28 CoreFoundation 0x7ff81562a8a9 CFRunLoopRunSpecific + 567 29 HIToolbox 0x7ff81e6b64f1 RunCurrentEventLoopInMode + 292 30 HIToolbox 0x7ff81e6b6118 ReceiveNextEventCommon + 284 31 HIToolbox 0x7ff81e6b5fe5 _BlockUntilNextEventMatchingListInModeWithFilter + 70 32 AppKit 0x7ff817f80d88 _DPSNextEvent + 886 33 AppKit 0x7ff817f7f3f4 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1411 34 AppKit 0x7ff817f71919 -[NSApplication run] + 586 35 libqcocoa.dylib 0x1168d862f QCocoaEventDispatcher::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) + 2495 36 QtCore 0x10d6a5acf QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) + 431 37 QtCore 0x10d6aa042 QCoreApplication::exec() + 130 38 QtWidgets.abi3.so 0x10eae40c2 meth_QApplication_exec_(_object*, _object*) + 82 39 Python 0x1020245c2 cfunction_call_varargs + 290 40 Python 0x102023f85 _PyObject_MakeTpCall + 373 41 Python 0x1020f2945 call_function + 533 42 Python 0x1020ef74e _PyEval_EvalFrameDefault + 25678 43 Python 0x1020248d0 function_code_fastcall + 128 44 Python 0x1020f28ec call_function + 444 45 Python 0x1020ef72a _PyEval_EvalFrameDefault + 25642 46 Python 0x1020f3734 _PyEval_EvalCodeWithName + 2804 47 Python 0x102024a6e _PyFunction_Vectorcall + 270 48 Python 0x102023dd7 _PyObject_FastCallDict + 247 49 Python 0x1020253df _PyObject_Call_Prepend + 143 50 Python 0x10207b5e1 slot_tp_init + 145 51 Python 0x102076a29 type_call + 297 52 Python 0x102023f85 _PyObject_MakeTpCall + 373 53 Python 0x1020f2945 call_function + 533 54 Python 0x1020ef7e5 _PyEval_EvalFrameDefault + 25829 55 Python 0x1020248d0 function_code_fastcall + 128 56 Python 0x1020f28ec call_function + 444 57 Python 0x1020ef72a _PyEval_EvalFrameDefault + 25642 58 Python 0x1020248d0 function_code_fastcall + 128 59 Python 0x1020f28ec call_function + 444 60 Python 0x1020ef72a _PyEval_EvalFrameDefault + 25642 61 Python 0x1020f3734 _PyEval_EvalCodeWithName + 2804 62 Python 0x102024a6e _PyFunction_Vectorcall + 270 63 Python 0x102023dd7 _PyObject_FastCallDict + 247 64 Python 0x1020253df _PyObject_Call_Prepend + 143 65 Python 0x10207b5e1 slot_tp_init + 145 66 Python 0x102076a29 type_call + 297 67 Python 0x102023f85 _PyObject_MakeTpCall + 373 68 Python 0x1020f2945 call_function + 533 69 Python 0x1020ef7e5 _PyEval_EvalFrameDefault + 25829 70 Python 0x1020248d0 function_code_fastcall + 128 71 Python 0x1020f28ec call_function + 444 72 Python 0x1020ef7e5 _PyEval_EvalFrameDefault + 25829 73 Python 0x1020f3734 _PyEval_EvalCodeWithName + 2804 74 Python 0x1020e9224 PyEval_EvalCode + 100 75 EZcardio 0x100f71a39 0x100f6d000 + 19001 76 EZcardio 0x100f721e7 0x100f6d000 + 20967 77 dyld 0x10f7204fe start + 462 Thread 1: 0 libsystem_pthread.dylib 0x7ff815561fec start_wqthread + 0 Thread 2: 0 libsystem_pthread.dylib 0x7ff815561fec start_wqthread + 0 Thread 3: 0 libsystem_pthread.dylib 0x7ff815561fec start_wqthread + 0 Thread 4:: com.apple.NSEventThread 0 libsystem_kernel.dylib 0x7ff815529aba mach_msg_trap + 10 1 libsystem_kernel.dylib 0x7ff815529e2b mach_msg + 59 2 CoreFoundation 0x7ff81562cdf1 __CFRunLoopServiceMachPort + 319 3 CoreFoundation 0x7ff81562b4af __CFRunLoopRun + 1329 4 CoreFoundation 0x7ff81562a8a9 CFRunLoopRunSpecific + 567 5 AppKit 0x7ff8180eefd8 _NSEventThread + 132 6 libsystem_pthread.dylib 0x7ff8155664f4 _pthread_start + 125 7 libsystem_pthread.dylib 0x7ff81556200f thread_start + 15 Thread 5: 0 libsystem_pthread.dylib 0x7ff815561fec start_wqthread + 0 Thread 6: 0 libsystem_pthread.dylib 0x7ff815561fec start_wqthread + 0 Thread 7: 0 libsystem_pthread.dylib 0x7ff815561fec start_wqthread + 0
-
@guillermollopis If you want to show the dialog as a blocking one then you should do
def request_user_input_database_and_file(self): mainWindow = MyMainImport(self) # Why mainWindow if it is a dialog?! mainWindow.exec()
instead of doing strange hacks.
-
@guillermollopis said in PyQT program crashes on Mac (not on Windows) when clicking on QComboBox:
Also, I just get the error when using Mac, not Windows
Well, it can work on one platform but fail on another.
Can you provide a minimal reproducible example showing this behaviour?
I could try on my Mac.