From cca2cf0ecdb8b4b9ffb50a6cdaa9a212238bb3f9 Mon Sep 17 00:00:00 2001 From: xiepengfei Date: Tue, 20 Oct 2026 19:56:39 +0800 Subject: [PATCH] fix: use DevicePixel for print page rect under Qt6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/widgets/window.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/widgets/window.cpp b/src/widgets/window.cpp index 5387b9645..b44006f3e 100644 --- a/src/widgets/window.cpp +++ b/src/widgets/window.cpp @@ -2721,7 +2721,7 @@ void Window::doPrint(DPrinter *printer, const QVector &pageRange) #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) QRectF pageRect(printer->pageRect()); #else - QRectF pageRect(printer->pageRect(QPrinter::Point)); + QRectF pageRect(printer->pageRect(QPrinter::DevicePixel)); #endif QRectF body = QRectF(0, 0, pageRect.width(), pageRect.height()); @@ -2827,7 +2827,7 @@ void Window::doPrintWithLargeDoc(DPrinter *printer, const QVector &pageRang #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) QRectF pageRect(printer->pageRect()); #else - QRectF pageRect(printer->pageRect(QPrinter::Point)); + QRectF pageRect(printer->pageRect(QPrinter::DevicePixel)); #endif QRectF body = QRectF(0, 0, pageRect.width(), pageRect.height()); // 使用首个文档信息即可 @@ -2923,7 +2923,7 @@ void Window::asynPrint(QPainter &p, DPrinter *printer, const QVector &pageR #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) QRectF pageRect(printer->pageRect()); #else - QRectF pageRect(printer->pageRect(QPrinter::Point)); + QRectF pageRect(printer->pageRect(QPrinter::DevicePixel)); #endif int dpiy = p.device()->logicalDpiY(); int margin = static_cast((2 / 2.54) * dpiy); // 2 cm margins