def display_image(self): self.flagFirstCircle = True self.image = qt.QImage(self.data, self.data.shape[1], self.data.shape[0], self.data.shape[1],qt.QImage.Format_Indexed8) self.image.setColorTable(self.colortable) pixMap = qt.QPixmap.fromImage(self.image) pixItem = qt.QGraphicsPixmapItem(pixMap) pixItem.setZValue(-1) self.scene.addItem(pixItem) self.scene.setSceneRect(0, 0, self.image.width(), self.image.height()) self.scene.update
def testSimple(): import sys import os filename = sys.argv[1] a = qt.QApplication(sys.argv) w = qt.QWidget() l = qt.QVBoxLayout(w) button = qt.QPushButton(w) button.setText("Print") scene = qt.QGraphicsScene() pixmapItem = qt.QGraphicsPixmapItem( qt.QPixmap.fromImage(qt.QImage(filename))) pixmapItem.setFlag(pixmapItem.ItemIsMovable, True) printer = qt.QPrinter(qt.QPrinter.HighResolution) printer.setFullPage(True) printer.setOutputFileName(os.path.splitext(filename)[0] + ".ps") page = qt.QGraphicsRectItem(0, 0, printer.width(), printer.height()) scene.setSceneRect(qt.QRectF(0, 0, printer.width(), printer.height())) scene.addItem(page) scene.addItem(pixmapItem) view = qt.QGraphicsView(scene) view.fitInView(page.rect(), qt.Qt.KeepAspectRatio) #view.setSceneRect( view.scale(2, 2) #page.scale(0.05, 0.05) def printFile(): painter = qt.QPainter(printer) scene.render( painter, qt.QRectF(0, 0, printer.width(), printer.height()), qt.QRectF(page.rect().x(), page.rect().y(), page.rect().width(), page.rect().height()), qt.Qt.KeepAspectRatio) painter.end() l.addWidget(button) l.addWidget(view) w.resize(300, 600) w.show() w.connect(button, qt.SIGNAL('clicked()'), printFile) a.exec_()
def addPixmap(self, pixmap, title=None, comment=None, commentposition=None): """ add a pixmap to the print preview scene """ if title is None: title = ' ' title += ' ' if comment is None: comment = ' ' comment += ' ' if commentposition is None: commentposition = "CENTER" if self.badNews: self.message.exec_() return rectItem = qt.QGraphicsRectItem(self.page, self.scene) scale = float(0.5 * self.scene.width() / pixmap.width()) rectItem.setRect(qt.QRectF(1, 1, pixmap.width(), pixmap.height())) pen = rectItem.pen() color = qt.QColor(qt.Qt.red) color.setAlpha(1) pen.setColor(color) rectItem.setPen(pen) rectItem.setZValue(1) rectItem.setFlag(qt.QGraphicsItem.ItemIsSelectable, True) rectItem.setFlag(qt.QGraphicsItem.ItemIsMovable, True) rectItem.setFlag(qt.QGraphicsItem.ItemIsFocusable, False) #I add the resize tool rectItemResizeRect = GraphicsResizeRectItem(rectItem, self.scene) rectItemResizeRect.setZValue(2) #I add a pixmap item pixmapItem = qt.QGraphicsPixmapItem(rectItem, self.scene) pixmapItem.setPixmap(pixmap) #pixmapItem.moveBy(0, 0) pixmapItem.setZValue(0) #I add the title textItem = qt.QGraphicsTextItem(title, rectItem, self.scene) textItem.setTextInteractionFlags(qt.Qt.TextEditorInteraction) offset = 0.5 * textItem.boundingRect().width() textItem.moveBy(0.5 * pixmap.width() - offset, -20) textItem.setZValue(2) #I add the comment commentItem = qt.QGraphicsTextItem(comment, rectItem, self.scene) commentItem.setTextInteractionFlags(qt.Qt.TextEditorInteraction) offset = 0.5 * commentItem.boundingRect().width() if commentposition.upper() == "LEFT": x = 1 else: x = 0.5 * pixmap.width() - offset commentItem.moveBy(x, pixmap.height() + 20) commentItem.setZValue(2) #I should adjust text size here #textItem.scale(2,2) #commentItem.scale(2,2) rectItem.scale(scale, scale) rectItem.moveBy(20, 40)