python - 是否可以从 QPrintDialog 调用的 QPrinter 对话框中取消选中“打印到文件”?
问题描述
我正在尝试使用 QPrinterDialog 打印 pdf 文件,并禁用对话框中的“打印到文件”选项。
有一些方法可以启用/禁用此选项,但不能取消选中它。是否可以使用 qt 框架务实地取消选中此选项?
解决方案
似乎“打印到文件”选项仅在您连接了打印机但目前我没有它时才会出现,所以我的解决方案没有经过测试。
一种可能的解决方案是使用文本名称过滤窗口的子项以获得 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"))
推荐阅读
- junit - Apache Karaf 在关闭 Pax-Exam 时卡住了
- c# - AngleSharp 点击 div
- c# - 如何在 mvc foreach 循环中应用样式 Css
- php - 如何使用 PHP 在数组列表中添加重复相同的值
- javascript - Next.js - 导入 css 文件不起作用
- c# - 选中项目时标记项目
- html - HTML 中的 SVG 字形
- batch-file - CMD突然打开和关闭。(批处理文件编码)
- jenkins - 在 jenkins 中配置 BEA Weblogic 工作区
- azure-data-factory - Lookup and foreach to Web activity in Azure Data factory: @Item() returning null