Skip to content

fix: remove device pixel ratio scaling in drag pixmap#417

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
Kakueeen:master
Feb 2, 2026
Merged

fix: remove device pixel ratio scaling in drag pixmap#417
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
Kakueeen:master

Conversation

@Kakueeen
Copy link
Contributor

Removed device pixel ratio scaling from drag pixmap creation to fix
incorrect rendering on high DPI displays. The scaling was causing the
drag preview image to appear too small and misaligned. Changes include:

  1. Remove ratio calculation and usage in image dimensions
  2. Simplify width/height calculations without DPI scaling
  3. Fix rounded rectangle coordinates to use proper pixel values
  4. Maintain image quality while ensuring correct display size

Log: Fixed drag tab preview display issue on high DPI screens

Influence:

  1. Test tab dragging on both standard and high DPI displays
  2. Verify drag preview image size matches expected dimensions
  3. Check that rounded corners render correctly
  4. Confirm image quality remains acceptable during drag operations
  5. Test on different display scaling settings (100%, 150%, 200%)

fix: 修复拖拽标签页预览在高DPI显示下的问题

移除了拖拽图片创建中的设备像素比缩放,以修复在高DPI显示器上的不正确渲染
问题。之前的缩放导致拖拽预览图片显示过小且错位。修改包括:

  1. 移除图像尺寸中的比例计算和使用
  2. 简化宽度/高度计算,不使用DPI缩放
  3. 修复圆角矩形坐标使用正确的像素值
  4. 保持图像质量的同时确保正确的显示尺寸

Log: 修复高DPI屏幕下拖拽标签页预览显示问题

Influence:

  1. 在标准和高DPI显示器上测试标签页拖拽功能
  2. 验证拖拽预览图片尺寸是否符合预期
  3. 检查圆角渲染是否正确
  4. 确认拖拽操作期间图像质量保持可接受
  5. 在不同显示缩放设置下测试(100%、150%、200%)

Removed device pixel ratio scaling from drag pixmap creation to fix
incorrect rendering on high DPI displays. The scaling was causing the
drag preview image to appear too small and misaligned. Changes include:
1. Remove ratio calculation and usage in image dimensions
2. Simplify width/height calculations without DPI scaling
3. Fix rounded rectangle coordinates to use proper pixel values
4. Maintain image quality while ensuring correct display size

Log: Fixed drag tab preview display issue on high DPI screens

Influence:
1. Test tab dragging on both standard and high DPI displays
2. Verify drag preview image size matches expected dimensions
3. Check that rounded corners render correctly
4. Confirm image quality remains acceptable during drag operations
5. Test on different display scaling settings (100%, 150%, 200%)

fix: 修复拖拽标签页预览在高DPI显示下的问题

移除了拖拽图片创建中的设备像素比缩放,以修复在高DPI显示器上的不正确渲染
问题。之前的缩放导致拖拽预览图片显示过小且错位。修改包括:
1. 移除图像尺寸中的比例计算和使用
2. 简化宽度/高度计算,不使用DPI缩放
3. 修复圆角矩形坐标使用正确的像素值
4. 保持图像质量的同时确保正确的显示尺寸

Log: 修复高DPI屏幕下拖拽标签页预览显示问题

Influence:
1. 在标准和高DPI显示器上测试标签页拖拽功能
2. 验证拖拽预览图片尺寸是否符合预期
3. 检查圆角渲染是否正确
4. 确认拖拽操作期间图像质量保持可接受
5. 在不同显示缩放设置下测试(100%、150%、200%)
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.

Sorry @Kakueeen, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot
Copy link

deepin pr auto review

这段代码修改涉及到了 Qt 中高 DPI(High DPI)屏幕适配的处理,主要逻辑是生成拖拽标签页时的预览图。以下是对这段 diff 的详细审查:

1. 语法逻辑审查

  • 修改点:移除了 qApp->devicePixelRatio() 的获取和使用,直接使用 textEdit 的原始宽高。
  • 逻辑分析
    • 原代码逻辑:原代码考虑了设备像素比(DPR),在创建 QImage 时使用的是物理像素尺寸(width * ratio),并设置了 setDevicePixelRatio。这意味着 QImage 内部存储的像素点更多,但在逻辑坐标系下大小不变。但在后续绘制圆角矩形时,代码使用了 scaledWidth / ratio,这在逻辑上试图将物理像素转回逻辑像素,但此时 scaledWidth 已经是经过缩放后的较小尺寸,除以 ratio 会导致尺寸变得非常小,这是一个逻辑 Bug
    • 新代码逻辑:新代码完全移除了 ratio 的计算,直接使用逻辑像素尺寸进行截图和绘制。这样逻辑上更加自洽:获取逻辑尺寸 -> 截图 -> 缩放 -> 绘制圆角。所有操作都在统一的逻辑坐标系下进行。
  • 结论:新代码修复了原代码在绘制圆角矩形时尺寸计算错误的逻辑 Bug,逻辑更加清晰正确。

2. 代码质量审查

  • 调试代码残留:代码中保留了 qDebug() << "Enter createDragPixmapFromTab, index:" << index;。建议在正式发布版本中移除或使用 qCDebug 配合日志分类进行管理,以免在控制台输出过多调试信息影响性能。
  • 类型安全widthheight 被定义为 int,而 devicePixelRatio() 返回 qreal(浮点数)。虽然新代码移除了浮点运算,但在涉及图形计算时,使用浮点数通常能获得更精确的缩放效果(尽管在这个缩放 1/5 的场景下影响不大)。
  • 空指针风险:代码中存在 window->wrapper(fileAt(index))wrapper->textEditor() 的调用。如果 window 为空,或者 wrapper 为空,或者 fileAt(index) 返回的文件对应的 wrapper 不存在,程序将崩溃。建议增加空指针检查(例如使用 qobject_cast 并判断结果,或直接判断指针)。

3. 代码性能审查

  • 渲染性能
    • textEdit->render(...):这是一个开销较大的操作,因为它会调用 QWidget 的绘制引擎。原代码和新代码都调用了它,性能差异主要在于渲染的分辨率。
    • 分辨率变化:原代码渲染的是高分辨率图像(物理像素),然后缩小;新代码直接渲染低分辨率图像(逻辑像素)。在 4K 屏(DPR=2.0)下,原代码处理的像素数是新代码的 4 倍。新代码显著减少了 renderscaled 操作的内存消耗和 CPU 计算量,提升了性能
  • 缩放质量Qt::SmoothTransformation 是双线性或双三次插值,质量较好。由于新代码是从较小的原始尺寸(逻辑像素)进行缩放,相比从高清图缩放,可能会丢失一些细节,但对于拖拽时的缩略图来说,这种细节损失通常是可以接受的,且换取了性能提升。

4. 代码安全审查

  • 整数溢出:虽然移除了 ratio 乘法降低了数值过大的风险,但如果 textEdit 的尺寸极大(例如数万像素),width * ratiowidth 本身仍可能导致整数溢出。不过这种情况在实际 GUI 应用中极少见。
  • 空指针解引用:如前所述,windowwrapper 的获取链条缺乏保护。如果 Tabbar 没有父窗口,或者父窗口不是 Window 类型,static_cast 将导致未定义行为(通常是崩溃)。建议改为 qobject_cast<Window *>(this->window()) 并检查返回值。

改进建议

  1. 移除调试日志

    // qDebug() << "Enter createDragPixmapFromTab, index:" << index;
  2. 增加空指针检查

    Window *window = qobject_cast<Window *>(this->window());
    if (!window) return QPixmap(); // 或者返回默认图标
    
    EditWrapper *wrapper = window->wrapper(fileAt(index));
    if (!wrapper) return QPixmap();
    
    TextEdit *textEdit = wrapper->textEditor();
    if (!textEdit) return QPixmap();
  3. 使用浮点数计算(可选)
    如果为了更精确的 1/5 缩放,可以使用 qreal 进行中间计算,最后再转为 int

    qreal scale = 0.2; // 1/5
    int scaledWidth = qRound(width * scale);
    int scaledHeight = qRound(height * scale);
  4. 关于 DPR 的处理
    目前的修改是完全忽略 DPR。如果希望在 DPR 较高的屏幕上生成更清晰的缩略图(但不想像原代码那样有 Bug),可以采取以下策略:

    • 保持截图使用 DPR(高分辨率),确保清晰度。
    • 修正后续缩放和圆角计算,统一使用逻辑坐标或统一使用物理坐标,不要混用。
    • 但考虑到这是一个拖拽缩略图,当前的修改(忽略 DPR,统一使用逻辑坐标)在性能和代码简洁性上是更好的选择,只要能接受在高分屏上缩略图稍微模糊一点(实际上通常看不出来区别)。

总结

这段代码修改在逻辑上修复了原代码的 Bug(圆角尺寸计算错误),在性能上有所提升(减少了像素处理量),在代码质量上需要补充空指针检查和移除调试日志。总体上是一个正向的优化

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

@Kakueeen
Copy link
Contributor Author

Kakueeen commented Feb 2, 2026

/forcemerge

@deepin-bot deepin-bot bot merged commit 488195f into linuxdeepin:master Feb 2, 2026
21 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