Skip to content

fix: Fix the issue where the input box cannot lose focus#356

Merged
pengfeixx merged 1 commit intolinuxdeepin:develop/snipefrom
pengfeixx:fix-319239
Jan 22, 2026
Merged

fix: Fix the issue where the input box cannot lose focus#356
pengfeixx merged 1 commit intolinuxdeepin:develop/snipefrom
pengfeixx:fix-319239

Conversation

@pengfeixx
Copy link
Contributor

@pengfeixx pengfeixx commented Jan 21, 2026

Fix the issue where the input box cannot lose focus

Log: Fix the issue where the input box cannot lose focus

Summary by Sourcery

Bug Fixes:

  • Fix input fields on the compression settings page not losing focus when clicking on empty areas of the page.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 21, 2026

Reviewer's Guide

Adds a mouse press handler on CompressSettingPage to clear focus from various input fields when the user clicks on the blank area, and updates the class interface to declare this new event handler as a protected override while moving isOrderMode() to the public section.

Sequence diagram for clearing input focus on blank area click

sequenceDiagram
    actor User
    participant CompressSettingPage
    participant FileNameEdt
    participant SavePathEdt
    participant PasswordEdt
    participant SplitValueEdt
    participant CommentEdt
    participant DWidget

    User->>CompressSettingPage: mousePressEvent(event)
    activate CompressSettingPage
    CompressSettingPage->>FileNameEdt: lineEdit().hasFocus()
    alt FileNameEdt has focus
        CompressSettingPage->>FileNameEdt: lineEdit().clearFocus()
    end
    CompressSettingPage->>SavePathEdt: lineEdit().hasFocus()
    alt SavePathEdt has focus
        CompressSettingPage->>SavePathEdt: lineEdit().clearFocus()
    end
    CompressSettingPage->>PasswordEdt: lineEdit().hasFocus()
    alt PasswordEdt has focus
        CompressSettingPage->>PasswordEdt: lineEdit().clearFocus()
    end
    CompressSettingPage->>SplitValueEdt: hasFocus()
    alt SplitValueEdt has focus
        CompressSettingPage->>SplitValueEdt: clearFocus()
    end
    CompressSettingPage->>CommentEdt: hasFocus()
    alt CommentEdt has focus
        CompressSettingPage->>CommentEdt: clearFocus()
    end
    CompressSettingPage->>DWidget: mousePressEvent(event)
    deactivate CompressSettingPage
Loading

Updated class diagram for CompressSettingPage

classDiagram
    class DWidget

    class CompressSettingPage {
        +QString getComment()
        +bool isOrderMode()
        +bool eventFilter(QObject watched, QEvent event)
        #void mousePressEvent(QMouseEvent event)
        -DLineEdit m_pFileNameEdt
        -DLineEdit m_pSavePathEdt
        -DLineEdit m_pPasswordEdt
        -QLineEdit m_pSplitValueEdt
        -QTextEdit m_pCommentEdt
    }

    CompressSettingPage --|> DWidget
Loading

File-Level Changes

Change Details Files
Clear focus from specific input widgets when clicking on the page background by handling mouse press events.
  • Implement mousePressEvent override in CompressSettingPage to intercept mouse clicks
  • For each relevant input widget, check for null and focus state before calling clearFocus
  • Delegate unhandled mouse press processing to the base DWidget implementation
src/source/page/compresssettingpage.cpp
Expose mousePressEvent override and adjust method visibility in the CompressSettingPage header.
  • Declare isOrderMode() in the public section before eventFilter
  • Introduce protected section and declare mousePressEvent(QMouseEvent *event) override with documentation
  • Keep eventFilter marked as override in the public API
src/source/page/compresssettingpage.h

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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:

  • Clearing focus on every mouse press may interfere with expected behavior when clicking inside other interactive child widgets; consider checking whether the click occurred on a genuinely empty/outer area (e.g., via childAt or checking the event target) before clearing focus.
  • The mousePressEvent currently logs with qDebug() on every click when a field has focus; if this is only for debugging the original issue, consider removing or guarding these logs to avoid noisy output in normal use.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Clearing focus on every mouse press may interfere with expected behavior when clicking inside other interactive child widgets; consider checking whether the click occurred on a genuinely empty/outer area (e.g., via `childAt` or checking the event target) before clearing focus.
- The `mousePressEvent` currently logs with `qDebug()` on every click when a field has focus; if this is only for debugging the original issue, consider removing or guarding these logs to avoid noisy output in normal use.

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.

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

Fix the issue where the input box cannot lose focus

Log: Fix the issue where the input box cannot lose focus
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码主要实现了在 CompressSettingPage 类中添加鼠标点击事件处理,当用户点击页面空白区域时,清除各个输入框的焦点。以下是对这段代码的审查意见,包括语法逻辑、代码质量、性能和安全方面的改进建议:

1. 语法逻辑

  1. 头文件中的注释错误

    • compresssettingpage.h 中,mousePressEvent 的注释是正确的,但 isOrderMode() 函数的注释错误地出现在了 protected 区域,而该函数实际上被移动到了 privatepublic 区域(根据 diff 无法确定,但注释位置不对)。
    • 建议:确保注释与函数定义的位置一致,避免误导。
  2. isOrderMode() 函数的可见性

    • 在 diff 中,isOrderMode() 从原来的位置被移动到了 protected 区域,但注释仍然保留在原来的位置。如果该函数不需要被子类访问,应该放在 private 区域。
    • 建议:根据实际需求决定 isOrderMode() 的可见性,并确保注释位置正确。

2. 代码质量

  1. 重复代码

    • mousePressEvent 中有多个类似的 if 语句,用于检查和清除不同输入框的焦点。这种重复代码可以通过重构来减少。
    • 建议:将输入框的检查和清除焦点逻辑封装成一个辅助函数,例如:
      void clearFocusIfHasFocus(QWidget *widget) {
          if (widget && widget->hasFocus()) {
              qDebug() << "Clearing focus from" << widget->objectName();
              widget->clearFocus();
          }
      }
      然后在 mousePressEvent 中调用:
      clearFocusIfHasFocus(m_pFileNameEdt->lineEdit());
      clearFocusIfHasFocus(m_pSavePathEdt->lineEdit());
      clearFocusIfHasFocus(m_pPasswordEdt->lineEdit());
      clearFocusIfHasFocus(m_pSplitValueEdt);
      clearFocusIfHasFocus(m_pCommentEdt);
  2. 调试信息过多

    • qDebug() 在生产环境中可能会影响性能,且过多的调试信息不利于代码维护。
    • 建议:使用条件编译或日志级别控制调试信息,例如:
      #ifdef QT_DEBUG
          qDebug() << "Clearing focus from file name edit";
      #endif
  3. 空指针检查

    • 代码中已经对指针进行了空指针检查(如 m_pFileNameEdt && m_pFileNameEdt->lineEdit()->hasFocus()),这是好的实践。
    • 建议:保持这种检查,但可以进一步封装以减少重复。

3. 代码性能

  1. 事件处理效率

    • mousePressEvent 会在每次鼠标点击时触发,如果页面中有大量输入框,可能会影响性能。
    • 建议:如果输入框数量很多,可以考虑使用事件过滤器或集中管理输入框的焦点状态。
  2. 调试输出性能

    • 频繁的 qDebug() 调用可能会影响性能,尤其是在高频事件(如鼠标点击)中。
    • 建议:如前所述,使用条件编译或日志级别控制。

4. 代码安全

  1. 指针安全性

    • 代码中已经对指针进行了空指针检查,这是好的实践。
    • 建议:确保所有指针在使用前都进行了初始化和有效性检查。
  2. 事件传递

    • mousePressEvent 最后调用了 DWidget::mousePressEvent(event),确保事件正确传递给父类,这是正确的。
    • 建议:保持这种调用,避免事件处理中断。

改进后的代码示例

compresssettingpage.h

protected:
    /**
     * @brief mousePressEvent 鼠标点击事件,点击空白区域取消输入框焦点
     * @param event
     */
    void mousePressEvent(QMouseEvent *event) override;

private:
    /**
     * @brief clearFocusIfHasFocus 如果控件有焦点,则清除焦点
     * @param widget
     */
    void clearFocusIfHasFocus(QWidget *widget);

compresssettingpage.cpp

void CompressSettingPage::clearFocusIfHasFocus(QWidget *widget)
{
    if (widget && widget->hasFocus()) {
#ifdef QT_DEBUG
        qDebug() << "Clearing focus from" << widget->objectName();
#endif
        widget->clearFocus();
    }
}

void CompressSettingPage::mousePressEvent(QMouseEvent *event)
{
    clearFocusIfHasFocus(m_pFileNameEdt ? m_pFileNameEdt->lineEdit() : nullptr);
    clearFocusIfHasFocus(m_pSavePathEdt ? m_pSavePathEdt->lineEdit() : nullptr);
    clearFocusIfHasFocus(m_pPasswordEdt ? m_pPasswordEdt->lineEdit() : nullptr);
    clearFocusIfHasFocus(m_pSplitValueEdt);
    clearFocusIfHasFocus(m_pCommentEdt);
    
    DWidget::mousePressEvent(event);
}

总结

  1. 语法逻辑:确保注释和函数定义的位置一致,避免误导。
  2. 代码质量:减少重复代码,封装通用逻辑,控制调试信息输出。
  3. 代码性能:优化事件处理逻辑,避免不必要的调试输出。
  4. 代码安全:保持空指针检查,确保事件正确传递。

通过以上改进,代码的可读性、可维护性和性能都会得到提升。

@pengfeixx pengfeixx merged commit 80b6b0b into linuxdeepin:develop/snipe Jan 22, 2026
15 checks passed
@pengfeixx pengfeixx deleted the fix-319239 branch January 22, 2026 01:44
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