From cabe80d794606d91fd47202a686423768cfdb0ac Mon Sep 17 00:00:00 2001 From: yeshanshan Date: Fri, 21 Aug 2026 17:13:42 +0800 Subject: [PATCH] fix: apply selected paper size after printer settings rebuild MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Fix issue where printing preview's default custom paper size was not synchronized to printer parameters 2. Add explicit matchFitablePageSize() call in _q_printerChanged() after paper list rebuild 3. The combo-box signal was not guaranteed to fire when the current index remained unchanged after rebuilding the paper list 4. Add unit test verifying the paper size is applied and printer parameters are updated correctly Log: Fixed printing preview issue where custom paper size was not synced to printer parameters Influence: 1. Verify printing preview opens with default custom paper size correctly applied 2. Test printer switching while a custom paper size is selected 3. Confirm the printer parameters reflect the selected paper size after printer changes 4. Test with various paper sizes including custom and standard formats 5. Verify paper size selection remains consistent between the dialog and printer settings fix: 修复打印预览默认自定义纸张未同步到打印机参数的问题 1. 修复打印预览中默认自定义纸张未同步到打印机参数的问题 2. 在 _q_printerChanged() 中纸张列表重建后显式调用 matchFitablePageSize() 3. 当重建纸张列表后当前索引未改变时,下拉框信号可能不会触发 4. 添加单元测试验证纸张大小正确应用且打印机参数同步更新 Log: 修复打印预览中自定义纸张大小未同步到打印机参数的问题 Influence: 1. 验证打印预览以默认自定义纸张大小正常打开 2. 测试在选中自定义纸张时切换打印机 3. 确认打印机切换后打印机参数反映所选纸张大小 4. 测试各种纸张大小,包括自定义和标准格式 5. 验证对话框和打印机设置之间纸张大小选择保持一致 --- src/widgets/dprintpreviewdialog.cpp | 3 +++ .../printpreview/ut_dprintpreviewdialog.cpp | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/widgets/dprintpreviewdialog.cpp b/src/widgets/dprintpreviewdialog.cpp index 840bc60c4..5da830a36 100644 --- a/src/widgets/dprintpreviewdialog.cpp +++ b/src/widgets/dprintpreviewdialog.cpp @@ -1802,6 +1802,9 @@ void DPrintPreviewDialogPrivate::_q_printerChanged(int index) } } + // The current index may not change when the paper list is rebuilt, so the + // combo-box signal is not guaranteed to apply the selected page size. + matchFitablePageSize(); marginsUpdate(true); paperSizeCombo->blockSignals(false); if (isInited) diff --git a/tests/testcases/printpreview/ut_dprintpreviewdialog.cpp b/tests/testcases/printpreview/ut_dprintpreviewdialog.cpp index 8f1d7e067..c571880b7 100644 --- a/tests/testcases/printpreview/ut_dprintpreviewdialog.cpp +++ b/tests/testcases/printpreview/ut_dprintpreviewdialog.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2021 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: LGPL-3.0-or-later @@ -338,6 +338,21 @@ TEST_F(ut_DPrintPreviewDialog, testPrintDeviceCombo) ASSERT_STREQ(test_dialog_d->pageRangeCombo->currentText().toLocal8Bit(), "All"); } +TEST_F(ut_DPrintPreviewDialog, testPaperSizeAppliedAfterPrinterChanged) +{ + test_dialog_d->paperSizeCombo->setCurrentText("8K"); + ASSERT_EQ(test_dialog_d->printer->pageSize(), QPrinter::Custom); + + // Rebuild the paper-size list while preserving the selected custom size. + // This path blocks the combo-box signal, so the page size must be applied + // explicitly after the selection is restored. + test_dialog_d->printer->setPageSize(QPageSize::A4); + test_dialog_d->_q_printerChanged(test_dialog_d->printDeviceCombo->currentIndex()); + + ASSERT_STREQ(test_dialog_d->paperSizeCombo->currentText().toLocal8Bit(), "8K"); + ASSERT_EQ(test_dialog_d->printer->pageSize(), QPrinter::Custom); +} + TEST_F(ut_DPrintPreviewDialog, testCancelBtn) { ASSERT_TRUE(test_dialog_d->cancelBtn->isEnabled());