在空间qtextedit中重置文本格式

发布于 2025-01-26 08:43:46 字数 599 浏览 2 评论 0原文

在按下qtextedit之后,是否有很好的解决方案来重置文本格式(例如粗体)?

案例的示例:

boldtext notboldtext //按下Space按钮文本不再大胆了,


例如,我可以使用qwidget :: eventfilter(...)并检查内部

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

,但看起来很糟糕。

这就是我现在设置格式的方式:

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

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

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

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文