You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux下如何移除QPrintPreviewDialog唤起的QPrinter对话框中的Write to PDF选项?

How to Remove the "Write to PDF" Option from QPrinter Dialog in Linux

Absolutely feasible! The default QPrintPreviewDialog relies on the system's standard print dialog, which lists all available printers—including virtual ones like "Write to PDF". To filter this out, you need to take control of the print workflow and use a custom filtered print dialog. Here's how to implement this:

1. Subclass QPrintPreviewDialog to Override the Default Print Action

The core idea is to intercept the print trigger from the preview dialog, replace the default print dialog with your own filtered version, then proceed with the print job.

C++ Implementation

#include <QPrintPreviewDialog>
#include <QPrintDialog>
#include <QPrinterInfo>
#include <QAction>

class CustomPrintPreviewDialog : public QPrintPreviewDialog
{
    Q_OBJECT
public:
    explicit CustomPrintPreviewDialog(QPrinter *printer, QWidget *parent = nullptr)
        : QPrintPreviewDialog(printer, parent)
    {
        // Locate the default print action and rewire its trigger
        QAction* printAction = findChild<QAction*>("print");
        if (printAction) {
            disconnect(printAction, &QAction::triggered, this, &QPrintPreviewDialog::print);
            connect(printAction, &QAction::triggered, this, &CustomPrintPreviewDialog::onCustomPrint);
        }
    }

private slots:
    void onCustomPrint()
    {
        QPrintDialog printDialog(printer(), this);
        
        // Filter out the "Write to PDF" printer
        QList<QPrinterInfo> allPrinters = QPrinterInfo::availablePrinters();
        QList<QPrinterInfo> filteredPrinters;
        for (const QPrinterInfo& printerInfo : allPrinters) {
            // Adjust the string match based on your system's actual printer name
            if (!printerInfo.printerName().contains("Write to PDF", Qt::CaseInsensitive)) {
                filteredPrinters.append(printerInfo);
            }
        }
        
        // Set the filtered list to the dialog
        printDialog.setPrinterList(filteredPrinters);
        
        if (printDialog.exec() == QDialog::Accepted) {
            // Apply the selected printer and trigger print
            printer()->setPrinterName(printDialog.selectedPrinter().printerName());
            printPreviewWidget()->print();
        }
    }
};

PyQt5/PySide2 Implementation

from PyQt5.QtPrintSupport import QPrintPreviewDialog, QPrintDialog, QPrinterInfo
from PyQt5.QtWidgets import QAction

class CustomPrintPreviewDialog(QPrintPreviewDialog):
    def __init__(self, printer, parent=None):
        super().__init__(printer, parent)
        # Find and rewire the default print action
        print_action = self.findChild(QAction, "print")
        if print_action:
            print_action.triggered.disconnect(self.print)
            print_action.triggered.connect(self.on_custom_print)
    
    def on_custom_print(self):
        print_dialog = QPrintDialog(self.printer(), self)
        
        # Filter out unwanted printers
        all_printers = QPrinterInfo.availablePrinters()
        filtered_printers = [
            p for p in all_printers 
            if "Write to PDF" not in p.printerName()
        ]
        
        print_dialog.setPrinterList(filtered_printers)
        
        if print_dialog.exec_() == QPrintDialog.Accepted:
            self.printer().setPrinterName(print_dialog.selectedPrinter().printerName())
            self.printPreviewWidget().print()

2. Verify Printer Names First

Note that on some Linux distributions, the virtual PDF printer might be named differently (e.g., "Print to File" or a localized name). To get the exact name, run this quick check first:

C++

#include <QDebug>
for (const QPrinterInfo& info : QPrinterInfo::availablePrinters()) {
    qDebug() << "Printer Name:" << info.printerName();
}

Python

for p in QPrinterInfo.availablePrinters():
    print(f"Printer Name: {p.printerName()}")

Use the exact name from the output to adjust the filter string in your code.

3. Use the Custom Dialog

Replace your original QPrintPreviewDialog instance with this custom one, and you'll no longer see the "Write to PDF" option in the print settings dialog.

内容的提问来源于stack exchange,提问作者Piotr Aahahhkja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:39:31