Skip to content

Conversation

@lichaofan2008
Copy link
Contributor

When using QTimer for delayed execution, it is necessary to check for null pointers to avoid null pointer exceptions.

fix: ImageView空指针异常。

使用QTimer延迟执行时,需要判断空指针,避免空指针异常。

Bug: https://pms.uniontech.com/bug-view-334545.html

When using QTimer for delayed execution, it is necessary to check for null pointers to avoid null pointer exceptions.

fix: ImageView空指针异常。

使用QTimer延迟执行时,需要判断空指针,避免空指针异常。

Bug: https://pms.uniontech.com/bug-view-334545.html
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lichaofan2008, max-lvs

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

@lichaofan2008
Copy link
Contributor Author

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Oct 9, 2025

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 02fb3d8 into linuxdeepin:release/1050U2 Oct 9, 2025
4 of 5 checks passed
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码的审查意见如下:

  1. 代码逻辑和安全性改进:
  • 使用 QPointer 是一个很好的改进,可以防止在 lambda 执行时对象已被销毁的情况。
  • 建议在 lambda 表达式中捕获 imgSelf 而不是 m_imageview,因为 m_imageview 可能在 lambda 执行前就被销毁。
  • 建议将 lambda 表达式中的条件判断提取为单独的函数,提高代码可读性。
  1. 性能优化:
  • 使用 QTimer::singleShot(100) 的延迟可能不够灵活,建议考虑使用更合适的延迟时间或让这个时间可配置。
  • 可以考虑在图像加载完成后立即进行尺寸判断,而不是延迟100ms,这样可以提高响应速度。
  1. 代码质量改进:
  • lambda 表达式中的逻辑较长,建议拆分为多个函数,提高可维护性。
  • 变量命名可以更具描述性,例如 rect1 可以命名为 imageRect。
  • 注释可以更详细,解释为什么需要延迟100ms。
  1. 潜在问题:
  • m_imageview->width() > 0 的检查可能不够全面,建议同时检查 height() > 0。
  • 在 lambda 表达式中使用了多个 m_imageview 的方法调用,如果对象在执行过程中被销毁,这些调用可能会导致崩溃。

改进建议代码:

void MainWidget::openImage(const QImage &img, const QString &name)
{
    // ... 其他代码 ...
    
    if (m_imageview) {
        m_imageview->openFilterImage(img);
        
        // 使用 QPointer 安全地访问对象
        QPointer<ImageView> imageViewPtr(m_imageview);
        QTimer::singleShot(100, [this, imageViewPtr]() {
            if (imageViewPtr.isNull()) {
                qWarning() << "ImageView object was destroyed before processing";
                return;
            }

            // 处理图像大小调整逻辑
            adjustImageViewSize(imageViewPtr);
        });
    }
}

void MainWidget::adjustImageViewSize(QPointer<ImageView> imageView)
{
    if (!imageView || imageView.isNull()) {
        return;
    }

    const QRect imageRect = imageView->image().rect();
    const int viewWidth = imageView->width();
    const int viewHeight = imageView->height();
    
    if (viewWidth <= 0 || viewHeight <= 0) {
        qWarning() << "Invalid view dimensions";
        return;
    }

    // 判断是否需要适应窗口大小
    const bool shouldFitToWindow = (imageRect.width() >= viewWidth || 
                                  imageRect.height() >= viewHeight - 150);
    
    if (shouldFitToWindow) {
        // 适应窗口大小的逻辑
        // ...
    } else {
        // 适应图片大小的逻辑
        // ...
    }
}

这样的改进提高了代码的安全性、可读性和可维护性,同时减少了潜在的性能问题和崩溃风险。

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