Skip to content

Conversation

@yixinshark
Copy link

在这paintEvent中判断文本是否发生变化,如果变化了则调用DToolTip::setShowToolTip(this, false)重置状态, 确保Tooltip能显示最新的文本内容。为此在DLabelPrivate中增加了lastText成员变量用于记录上一次的文本。

Check if the text has changed in paintEvent. If it has changed, call DToolTip::setShowToolTip(this, false) to reset the status, ensuring that the Tooltip displays the latest text content. To achieve this, a lastText member variable was added to DLabelPrivate to record the previous text.

Log: fix tooltip does not show the latest text
Pms: BUG-300837

在这paintEvent中判断文本是否发生变化,如果变化了则调用DToolTip::setShowToolTip(this, false)重置状态,
确保Tooltip能显示最新的文本内容。为此在DLabelPrivate中增加了lastText成员变量用于记录上一次的文本。

Check if the text has changed in paintEvent. If it has changed, call DToolTip::setShowToolTip(this, false)
to reset the status, ensuring that the Tooltip displays the latest text content. To achieve this,
a lastText member variable was added to DLabelPrivate to record the previous text.

Log: fix tooltip does not show the latest text
Pms: BUG-300837
@deepin-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: yixinshark

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

@deepin-ci-robot
Copy link
Contributor

deepin pr auto review

这段代码的修改目的是为了解决 DLabel 在文本更新后工具提示可能无法正确显示新文本的问题。以下是对这段代码的详细审查意见,包括语法逻辑、代码质量、代码性能和代码安全方面:

1. 语法逻辑

  • 逻辑正确性:代码逻辑是正确的。在 paintEvent 中,每次绘制前检查当前文本 d->text 是否与上一次保存的文本 lastText 不同。如果不同,则调用 DToolTip::setShowToolTip(this, false) 来重置工具提示的显示状态,确保下次显示时能获取到最新的文本。
  • 更新时机lastText 的更新放在了重置工具提示之后,确保了比较的是"旧"文本,逻辑顺序正确。

2. 代码质量

  • 变量命名lastText 命名清晰,能够直观表达其用途。
  • 注释:添加的注释 // clear flag when text changed, avoid tooltip don't show new text 清晰地解释了代码的意图,值得肯定。
  • 代码位置:将 lastText 的更新逻辑放在 paintEvent 中是可以的,因为 paintEvent 是文本显示的最终环节。但 paintEvent 是一个高频调用的函数,这里增加逻辑需要考虑性能影响(见下文性能分析)。
  • 初始化问题lastTextQString 类型,默认初始化为空字符串。如果 DLabel 初始文本为空,第一次设置非空文本时,d->text != d_func()->lastText 会成立,逻辑正确。但如果初始文本非空,第一次绘制时也会触发重置逻辑,这通常是期望的行为。

3. 代码性能

  • 字符串比较开销QString 的比较操作(d->text != d_func()->lastText)在大多数情况下是高效的(先比较长度,再比较内容),但在 paintEvent 这种高频调用的函数中,如果文本非常长且频繁更新,可能会带来一定的性能开销。
  • 建议优化:如果文本更新不是非常频繁,当前的实现是可以接受的。但如果需要进一步优化,可以考虑在文本设置的地方(如 setText 函数)直接标记文本已更改,而不是在 paintEvent 中进行比较。例如:
    // 在 DLabelPrivate 中添加一个标志
    bool textChanged = false;
    
    // 在 setText 中设置标志
    void DLabel::setText(const QString &text) {
        D_D(DLabel);
        if (d->text != text) {
            d->text = text;
            d->textChanged = true;
            DToolTip::setShowToolTip(this, false);
            update();
        }
    }
    
    // 在 paintEvent 中检查标志
    if (d->textChanged) {
        d->textChanged = false;
        // 其他逻辑...
    }
    这样可以避免在 paintEvent 中进行字符串比较,但需要修改 setText 的实现。

4. 代码安全

  • 空指针检查d_func()DToolTip::setShowToolTip 的调用没有显式的空指针检查。通常 d_func() 是安全的(Qt 的 D-Pointer 模式保证),但 DToolTip::setShowToolTip 的安全性取决于其实现。
  • 线程安全DLabel 是 UI 组件,通常只在主线程操作,因此线程安全不是问题。
  • 内存安全QString 的赋值和比较是安全的,不会导致内存问题。

5. 其他建议

  • 功能扩展:如果未来需要更复杂的工具提示逻辑(例如动态工具提示内容),可以考虑将工具提示的更新逻辑封装成一个单独的函数,而不是直接在 paintEvent 中处理。
  • 测试覆盖:建议增加以下测试用例:
    • 文本从空变为非空。
    • 文本从非空变为空。
    • 文本频繁更新。
    • 文本内容不变但其他属性(如颜色)变化。

总结

这段代码的修改是合理的,能够解决工具提示更新不及时的问题。主要需要关注的是性能优化,尤其是在高频调用的 paintEvent 中增加逻辑可能带来的影响。如果文本更新不是非常频繁,当前的实现是可以接受的;否则,建议将逻辑移到 setText 中以减少 paintEvent 的开销。

@yixinshark
Copy link
Author

@18202781743

// clear flag when text changed, avoid tooltip don't show new text
DToolTip::setShowToolTip(this, false);
}
d_func()->lastText = d->text;
Copy link
Contributor

Choose a reason for hiding this comment

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

这个移动到判断里面去吧 ,

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