'Reset text formatting in QTextEdit after space

Are there good solution to reset text formatting (e.g. bold) after press Space button for QTextEdit?

Example of case:

boldText notBoldText // After press Space button text is not bold anymore


For example, I can use QWidget::eventFilter(...) and check inside

if (key == Qt::Key_Space) { ... }

But it looks bad.

It's how I set formatting now:

    QTextCursor cursor = textCursor();
    if (!cursor.hasSelection())
        return;

    QTextCharFormat fmt;
    fmt.setFontWeight(cursor.charFormat().font().bold() ? QFont::Normal : QFont::DemiBold);

    cursor.mergeCharFormat(format);
    mergeCurrentCharFormat(format);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source