zoukankan      html  css  js  c++  java
  • 菜单栏、工具栏与状态栏-QPrinter

    打印图像是图像处理软件中的一个常用功能。打印图像实际上是在QPaintDevice中画图,与平常在QWidget、QPixmap和QImage中画图一样,都是创建一个QPainter对象进行画图的,只是打印使用的是QPrinter,它本质上也是一个QPaintDevice(绘图设备)。

    案例37  QPrinter的使用

    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel, QSizePolicy, QAction
    from PyQt5.QtGui import QIcon, QImage, QPixmap, QPainter
    from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
    from PyQt5.QtCore import Qt
    
    
    class QPrinterDemo(QMainWindow):
        def __init__(self, parent=None):
            super().__init__(parent)
            self.setWindowTitle(self.tr("打印图片"))
            self.imageLabel = QLabel()
            self.imageLabel.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)
            self.setCentralWidget(self.imageLabel)
    
            self.image = QImage()
            self.createActions()
            self.createMenus()
            self.createToolBars()
    
            if self.image.load("./images/screen.png"):
                self.imageLabel.setPixmap(QPixmap.fromImage(self.image))
                self.resize(self.image.width(), self.image.height())
    
        def createActions(self):
            self.PrintAction = QAction(
                QIcon("./images/printer.png"),
                self.tr("打印"),
                self
            )
            self.PrintAction.setShortcut("Ctrl+P")
            self.PrintAction.setStatusTip(self.tr("打印"))
            self.PrintAction.triggered.connect(self.slotPrint)
    
        def createMenus(self):
            PrintMenu = self.menuBar().addMenu(self.tr("打印"))
            PrintMenu.addAction(self.PrintAction)
    
        def createToolBars(self):
            fileToolBar = self.addToolBar("Print")
            fileToolBar.addAction(self.PrintAction)
    
        def slotPrint(self):
            printer = QPrinter()
            printDialog = QPrintDialog(printer, self)
            if printDialog.exec_():
                painter = QPainter(printer)
                rect = painter.viewport()
                size = self.image.size()
                size.scale(rect.size(), Qt.KeepAspectRatio)
                painter.setViewport(rect.x(), rect.y(), size.width(), size.height())
                painter.setWindow(self.image.rect())
                painter.drawImage(0, 0, self.image)
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        demo = QPrinterDemo()
        demo.show()
        sys.exit(app.exec_())
  • 相关阅读:
    9-python 的ProxyHandler处理器(代理设置)
    2018.2.7 css 的一些方法盒子模型
    2018.2.6 JS-判断用户浏览器
    2018.2.5 PHP如何写好一个程序用框架
    2018. 2.4 Java中集合嵌套集合的练习
    2018.2.3 Centos 的vim好看的主题配置及JDK的安装配置
    2018.2.2 java中的Date如何获取 年月日时分秒
    2018.2.2 JavaScript中的封装
    2018.1.30 PHP编程之验证码
    2018.1.29 计算机二级错题汇总(二)
  • 原文地址:https://www.cnblogs.com/lynsha/p/13452438.html
Copyright © 2011-2022 走看看