Search code examples
qtqmlsuppress-warnings

How to disable specific QML debugger warning


I don't want to disable all warnings from QML (as asked in this question). Instead I want to disable a specific type of warning. In my case the TypeError: Cannot read property of null warning.

Note that I am getting this warning as a result of a Qt bug that affects grandchildren elements during their destruction, not as a result of any code errors, I believe. In my case, I get lots of these warnings (10s to 100s) each time a particular GridView model is changed, so these messages are dominating the console log.


Solution

  • I think a high level solution would probably be based on installing custom message handler and intercept all warnings, filter out any warning you like to deal with in a different way and bypass others, this for example can handle your case:

    // Default message handler to be called to bypass all other warnings.
    static const QtMessageHandler QT_DEFAULT_MESSAGE_HANDLER = qInstallMessageHandler(0);
    // a custom message handler to intercept warnings
    void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString & msg)
    {
        switch (type) {
        case QtWarningMsg: {
            if (!msg.contains("TypeError: Cannot read property of null")){ // suppress this warning
                (*QT_DEFAULT_MESSAGE_HANDLER)(type, context, msg); // bypass and display all other warnings
            }
        }
        break;
        default:    // Call the default handler.
            (*QT_DEFAULT_MESSAGE_HANDLER)(type, context, msg);
            break;
        }
    }
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        qInstallMessageHandler(customMessageHandler); // install custom msg handler
    ...
    }