Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QCoreApplication::sendEvent issue



  • Hello!

    I have the application which uses singleton pattern, and when emiting signal from the main window to this singleton class it displays the following issue:

    ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 0x0x199953e0f30. Receiver 'qt_scrollarea_viewport' (of type 'QWidget') was created in thread 0x0x19990c6b7d0", file kernel\qcoreapplication.cpp, line 563
    

    The application contains few QTreeWidgets and buttons. When I select the QTreeWidgetItem and click a button it emits the appropriate signal.

    Example code:

    connect(this, &MainWindow::mySignal, &Test::instance(), &Test::doSomething);
    
    emit mySignal(ui->treeWidget->currentItem()->text(0), ui->treeWidget->currentItem()->text(1), ui->treeWidget->currentItem()->text(5), ui->treeWidget->currentItem()->text(2), ui->treeWidget->currentItem()->text(7), ui->treeWidget->currentItem()->text(1), true, true);
    

    Also, this issue only appear in the Debug mode only. In the Release mode it works well. I use Qt 5.9.8 LTS. Any ideas how to fix it? Thanks.



  • Ok. I have found the problem. The issue is with Wlan API callback function, for example:

    When I check:

                       case wlan_notification_acm_connection_start:
                             if (wlanNotificationData->dwDataSize != sizeof(WLAN_CONNECTION_NOTIFICATION_DATA)) {
                                 emit instance().notifDataSizeFailed();
                                 break;
                             }
    
                             emit instance().apConnecting();
                             break;
    

    it emits signal and sets the button text to "Connecting". This code for some reason lead to ASSERT failure in QCoreApplication::sendEvent issue only in the Debug mode.



  • I can add some checks for this code:

    #ifndef QT_DEBUG
    
    #endif
    

    But I think there is a better solution. Thanks in advance for your help.



  • During some code investigation, I found that break; actually breaks the application event loop and can not return to it when new Wlan state is registered, the Wlan API callback function receives different states.

    For example:

    1. It gets the wlan_notification_acm_connection_start state
    2. Then it emits signal to displays button text, then it breaks from switch
    3. New state is registered from the Wlan callback function, for example, wlan_notification_acm_connection_complete, but it can not return to this event anymore because it made a break early (it broke not only from the switch but also from this event loop). Also, possible to say it broke from the connect.

    That is why, I changed it to if/else:

                     if (wlanNotificationData->NotificationCode == wlan_notification_acm_connection_start) {
                         if (wlanNotificationData->dwDataSize != sizeof(WLAN_CONNECTION_NOTIFICATION_DATA)) {
                             emit instance().notifDataSizeFailed();
                             return;
                         }
    
                         emit instance().apConnecting();
                     } else if (wlanNotificationData->NotificationCode == wlan_notification_acm_connection_complete) {
                        .....
                     }
    

    Now it works well in Debug/Release modes. The issue is resolved.


Log in to reply