Skip to content

fix(ctextedit): fix crash and Qt6 deprecated API usage#195

Merged
wyu71 merged 1 commit intolinuxdeepin:develop/snipefrom
wyu71:develop/snipe
Mar 12, 2026
Merged

fix(ctextedit): fix crash and Qt6 deprecated API usage#195
wyu71 merged 1 commit intolinuxdeepin:develop/snipefrom
wyu71:develop/snipe

Conversation

@wyu71
Copy link
Contributor

@wyu71 wyu71 commented Mar 12, 2026

Add empty check before calling first() on fmts to prevent crash. Use fontFamilies()/setFontFamilies() for Qt 6.1+ compatibility.

添加空检查防止fmts为空时崩溃,Qt 6.1+使用新API替代废弃接口。

Log: 修复文本编辑器崩溃问题及Qt6兼容性
PMS: BUG-352807
Influence: 修复选中空区域时可能崩溃的问题,解决Qt 6.1+编译时的废弃API警告。

Summary by Sourcery

Prevent text editor crashes on empty selection and update font family handling for Qt 6 compatibility.

Bug Fixes:

  • Avoid crashes in selection format updates by handling empty character format lists before accessing their first element.

Enhancements:

  • Use Qt 6.1+ font family APIs (fontFamilies/setFontFamilies) while preserving compatibility with older Qt versions.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Consider extracting the Qt 5/6 font family handling into a small helper (e.g., getCurrentFontFamily() / setFormatFontFamily() static functions) to avoid scattering version-specific #if logic across multiple methods.
  • In updateSelectionFormat(), resetting _selectionFmt to a default QTextCharFormat on empty fmts may change existing behavior; if the intention is just to avoid a crash, you might instead early-return without altering _selectionFmt to preserve the previous selection state.
  • The qDebug() logging in setCurrentFontFamily() could become noisy in normal usage; consider guarding it behind a debug flag or removing it if it's no longer needed for diagnosing this bug.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider extracting the Qt 5/6 font family handling into a small helper (e.g., `getCurrentFontFamily()` / `setFormatFontFamily()` static functions) to avoid scattering version-specific `#if` logic across multiple methods.
- In `updateSelectionFormat()`, resetting `_selectionFmt` to a default `QTextCharFormat` on empty `fmts` may change existing behavior; if the intention is just to avoid a crash, you might instead early-return without altering `_selectionFmt` to preserve the previous selection state.
- The qDebug() logging in `setCurrentFontFamily()` could become noisy in normal usage; consider guarding it behind a debug flag or removing it if it's no longer needed for diagnosing this bug.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@wyu71 wyu71 force-pushed the develop/snipe branch 2 times, most recently from eb11921 to 4df3972 Compare March 12, 2026 05:55
Add empty check before calling first() on fmts to prevent crash.
Use fontFamilies()/setFontFamilies() for Qt 6.1+ compatibility.

添加空检查防止fmts为空时崩溃,Qt 6.1+使用新API替代废弃接口。

Log: 修复文本编辑器崩溃问题及Qt6兼容性
PMS: BUG-352807
Influence: 修复选中空区域时可能崩溃的问题,解决Qt 6.1+编译时的废弃API警告。
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码主要是为了适配 Qt 6.1.0 及以上版本中关于字体处理 API 的变更,同时修复了一个潜在的空指针/空列表访问崩溃问题。以下是对这段代码的详细审查意见:

1. 语法与逻辑审查

  • Qt 版本兼容性处理

    • 代码正确使用了 #if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0) 宏来区分 Qt 6.1+ 和旧版本的 API。这是处理 Qt 版本升级的标准做法。
    • 在 Qt 6 中,QTextCharFormat::fontFamily() 被弃用,改为使用返回 QStringListfontFamilies()。代码逻辑正确:获取列表后取第一个元素作为当前字体族。
    • 同样,设置字体时也正确使用了 setFontFamilies(QStringList)
  • 空指针/空列表保护

    • updateSelectionFormat 函数中,增加了 if (fmts.isEmpty()) 的判断。
    • 逻辑正确性:原代码直接调用 fmts.first(),如果 fmts 为空(例如光标位于文档末尾且没有选区,或者文档为空时),会导致程序崩溃。新增的检查有效地防止了这种情况,逻辑严谨。

2. 代码质量审查

  • 代码风格

    • 变量命名清晰(如 fmt, ft, family)。
    • const 引用使用得当:在循环中将 QTextCharFormat ft = fmts.at(i).format; 修改为 const QTextCharFormat &ft = fmts.at(i).format;。这是一个很好的改进,避免了不必要的对象拷贝,提高了性能。
    • 缩进和空格保持良好,符合 Qt/C++ 的通用规范。
  • 注释

    • 新增的 //Check for empty to prevent crash... 注释非常有价值,解释了增加 isEmpty 检查的原因,有助于后续维护。

3. 代码性能审查

  • 对象拷贝优化
    • 如上所述,在 updateSelectionFormat 的循环中使用 const QTextCharFormat &ft 替代了值传递,减少了构造函数和析构函数的调用开销,尤其是在处理大量文本选区时,性能提升会有所体现。

4. 代码安全审查

  • 崩溃防护
    • 修复了 fmts.first() 可能引发的越界访问崩溃。这是最关键的安全改进。
    • currentFontFamily() 中,使用了 .value(0)。这也是一种安全的做法,如果 QStringList 为空,它会返回默认构造的 QString(即空字符串),而不会像 operator[](0) 那样导致未定义行为。

5. 改进建议

尽管代码整体质量很高,但仍有以下微小的优化或建议空间:

  1. 版权年份更新

    • SPDX-FileCopyrightText 从 2022 更新到了 2026。这通常是自动化的或为了长期维护。建议确认这是否符合项目实际规划。
  2. currentFontFamily 的健壮性

    • 虽然使用了 .value(0) 很安全,但在某些极端情况下(例如格式损坏),fontFamilies() 可能包含空字符串。如果业务逻辑对字体有效性要求极高,可以考虑在返回前检查字符串是否为空,或者提供一个默认回退字体(如系统默认字体)。
    • 示例代码(仅供参考):
      QString CTextEdit::currentFontFamily()
      {
      #if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0)
          QStringList families = currentFormat().fontFamilies().toStringList();
          // 如果列表为空或第一个元素为空,返回默认字体(可选逻辑)
          if (families.isEmpty() || families.first().isEmpty()) {
              return QFontDatabase::systemDefaultFont().family();
          }
          return families.first();
      #else
          return currentFormat().fontFamily();
      #endif
      }
  3. 日志输出

    • qDebug() << "Setting current font family:" << family; 在每次设置字体时都会打印。如果该函数被高频调用(例如在富文本编辑中实时处理格式),可能会产生大量日志,影响性能。建议考虑使用 qCDebug 并配合分类日志,或者在非调试模式下移除/减少此类日志。

总结

这段代码修改是高质量的。它成功地处理了 Qt 版本升级带来的 API 变更,并修复了一个可能导致崩溃的严重 Bug。修改后的代码在逻辑上更加严密,性能上也有细微优化。除了上述几点锦上添花的建议外,该代码可以直接合并。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, wyu71

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@wyu71 wyu71 merged commit 9d372e2 into linuxdeepin:develop/snipe Mar 12, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants