Skip to content

fix: use DevicePixel for print page rect under Qt6 - #672

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/qt6-print-page-rect-unit-master
Oct 20, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:fix/qt6-print-page-rect-unit-master

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Qt6 打印引擎使用打印机原生分辨率(setResolution() 已废弃无效),而 pageRect(QPrinter::Point) 返回磅(1/72 英寸)单位,与代码中按设备像素计算的页边距、字号、setPageSize 不一致,导致打印及打印预览排版错乱:文字过大、每行字符极少、总页数异常、页码框宽度为负不显示。

Changes

  • src/widgets/window.cpp 三处打印路径(doPrint / doPrintWithLargeDoc / asynPrint)将 pageRect(QPrinter::Point) 改为 pageRect(QPrinter::DevicePixel),恢复 Qt5 无参 pageRect() 的设备像素语义,与 painter 坐标系及边距/字号计算单位保持一致。

Testing

  • Qt 6.8(gcc 13)完整构建通过,window.cpp 编译无警告
  • 打印预览(Ctrl+P)检查 2cm 页边距与字号正常
  • 导出 PDF(A4)检查总页数与右下角页码
  • 大文件(>1MB)大文档打印路径翻页预览排版稳定

PMS: BUG-380145

Summary by Sourcery

Bug Fixes:

  • Use device-pixel page rectangles across Qt6 printing paths to restore consistent layout units and correct print and preview rendering.

1. Qt6 print engines use the printer's native resolution, not 72 DPI.
2. pageRect(Point) broke unit consistency: margins and fonts are in
   device pixels, so layout got wrong scale and negative title box.
3. Restore Qt5 pageRect() semantics by using DevicePixel in all 3
   print paths (doPrint, doPrintWithLargeDoc, asynPrint).

Log: Fixed broken Qt6 print layout: oversized text, wrong page count
and missing page numbers.

Influence:
1. Open a text file, press Ctrl+P, check 2cm margins and normal font
   size in print preview.
2. Export to PDF (A4) from the print dialog and verify total page
   count and bottom-right page numbers.
3. Open a large file (>1MB) to hit the large-doc print path and flip
   preview pages to check layout stability.

fix: 修复Qt6下打印页面矩形单位错误导致的排版异常

1. Qt6 打印引擎使用打印机原生分辨率,不再默认 72 DPI。
2. pageRect(Point) 与边距、字号的设备像素单位不一致,导致缩放
   错误且页码框宽度为负。
3. 三处打印路径(doPrint/doPrintWithLargeDoc/asynPrint)恢复 Qt5
   pageRect() 的设备像素语义。

Log: 修复 Qt6 下打印及打印预览排版错乱(文字过大、页数异常、
页码缺失)的问题。

Influence:
1. 打开文本文件,Ctrl+P 打印预览,检查 2 厘米页边距与字号是否
   正常。
2. 在打印对话框导出 PDF(A4),检查总页数与右下角页码是否
   正确。
3. 打开大文件(大于 1MB)走大文档打印路径,翻页预览检查排版
   稳定。

PMS: BUG-380145

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@sourcery-ai

sourcery-ai Bot commented Oct 20, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates all Qt6 printing paths to obtain page rectangles in device pixels, matching painter coordinates and existing margin, font-size, page-size, and pagination calculations; Qt5 behavior remains unchanged.

Sequence diagram for Qt6 device-pixel print layout

sequenceDiagram
    participant Window
    participant DPrinter
    participant QPainter
    Window->>DPrinter: pageRect(QPrinter::DevicePixel)
    DPrinter-->>Window: pageRect in device pixels
    Window->>QPainter: render page using matching coordinates
    QPainter-->>Window: print or preview output with consistent layout
Loading

File-Level Changes

Change Details Files
Align Qt6 print page geometry with the device-pixel coordinate system used by rendering and layout calculations.
  • Replace Qt6 pageRect(QPrinter::Point) calls with pageRect(QPrinter::DevicePixel) in all three printing paths.
  • Retain the Qt5 compatibility branch using the legacy no-argument pageRect() API.
  • Apply the unit correction consistently to normal printing, large-document printing, and asynchronous/preview rendering.
src/widgets/window.cpp

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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 100 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交将 Qt6 下打印页面矩形获取的单位从 QPrinter::Point 修改为 QPrinter::DevicePixel, 修复了 Qt6 下 pageRect 单位不正确的问题。变更涉及3处函数(doPrint、doPrintWithLargeDoc、asynPrint), 修改一致且语法正确,无安全漏洞,四维度均通过。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: []


💡 改进建议代码示例

// 当前修改已正确,无需额外修复。 // Qt6 下使用 DevicePixel 获取设备像素坐标的页面矩形: #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QRectF pageRect(printer->pageRect());
#else
    QRectF pageRect(printer->pageRect(QPrinter::DevicePixel));
#endif

本报告由 AI 代码审查工具自动生成

@deepin-ci-robot

Copy link
Copy Markdown

[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

@pengfeixx

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
deepin-bot Bot merged commit 578010d into linuxdeepin:master Oct 20, 2026
17 checks passed
@pengfeixx
pengfeixx deleted the fix/qt6-print-page-rect-unit-master branch October 20, 2026 12:41
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