是否可以从QPrintDialog调用的QPrinter对话框中取消选中“打印到文件”?

问题描述 投票:0回答:1

我正在尝试使用QPrinterDialog打印pdf文件,并在对话框中禁用“打印到文件”选项。

[有一些方法可以启用/禁用此选项,但不要取消选中它。是否可以使用qt framework实用地取消选中此选项?

python pyside2
1个回答
0
投票

似乎只有在连接了打印机的情况下,才会出现“打印到文件”选项,但此刻我没有,因此未测试我的解决方案。

一种可能的解决方案是使用文本名称过滤窗口的子级以获得QCheckBox:

from PySide2 import QtCore, QtGui, QtWidgets, QtPrintSupport


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    printer = QtPrintSupport.QPrinter()
    dialog = QtPrintSupport.QPrintDialog(printer)

    def on_timeout():
        for checkbox in dialog.findChildren(QtWidgets.QCheckBox):
            print(
                "objectName: {}, text: {}".format(
                    checkbox.objectName(), checkbox.text()
                )
            )
            if checkbox.text() == "Print to file":
                checkbox.setCheckState(QtCore.Qt.Unchecked)

    QtCore.QTimer.singleShot(0, on_timeout)

    if dialog.exec_() == QtWidgets.QDialog.Accepted:
        painter = QtGui.QPainter(printer)
        painter.fillRect(printer.pageRect(), QtGui.QColor("salmon"))
© www.soinside.com 2019 - 2024. All rights reserved.