Linux下如何移除QPrintPreviewDialog唤起的QPrinter对话框中的Write to PDF选项?
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

