Item-Based Graphics in Java Incoporate Code128 in Java Item-Based Graphics

12. Item-Based Graphics generate, create none none in none projects History of QR Code Standardization matrix = QMat none none rix() stream >> type >> position >> matrix if offset: position += QPointF(offset, offset) if type == "Text": text = QString() font = QFont() stream >> text >> font TextItem(text, position, self.scene, font, matrix) elif type == "Box": rect = QRectF() stream >> rect style = Qt.PenStyle(stream.

readInt16()) BoxItem(position, self.scene, style, rect, matrix) elif type == "Pixmap": pixmap = QPixmap() stream >> pixmap self.createPixmapItem(pixmap, position, matrix).

This method i s used both by paste() and by open() (which loads a .pgd le). It begins by reading in the type, position, and matrix which are stored for every type of item.

Then, it adjusts the position by the offset this is used only if the item is being pasted. Next, the item-speci c data is read and a suitable item created using the data that has been gathered. The TextItem and BoxItem initializers, and the createPixmapItem() method, all create the appropriate graphics items and pass ownership to the scene.

. def rotate(se none for none lf): for item in self.scene.selectedItems(): item.

rotate(30). If the user c licks Rotate, any selected items are rotated by 30 . There are no child items used in this application, but if any of the rotated items had child items, these too would be rotated..

def delete(se none none lf): items = self.scene.selectedItems() if len(items) and QMessageBox.

question(self, "Page Designer - Delete", "Delete %d item%s " % (len(items), "s" if len(items) != 1 else ""), QMessageBox.Yes. QMessageBox.N o) == QMessageBox.Yes: while items: item = items.

pop() self.scene.removeItem(item) del item global Dirty.

Custom and Interactive Graphics Items Dirty = True If the user c none for none licks Delete and there is at least one selected item, they are asked whether they want to delete the selected items, and if they do, each selected item is deleted.. def print_(se lf): dialog = QPrintDialog(self.printer) if dialog.exec_(): painter = QPainter(self.

printer) painter.setRenderHint(QPainter.Antialiasing) painter.

setRenderHint(QPainter.TextAntialiasing) self.scene.

clearSelection() self.removeBorders() self.scene.

render(painter) self.addBorders(). A QPrinter is none none a paint device, just like a QWidget or a QImage, so we can easily paint onto a printer. Here we have taken advantage of the QGraphicsScene.render() convenience method, which paints the entire scene (or a selected portion of it) onto a paint device.

Before painting, we remove the borders (the yellow rectangles), and after painting we restore the borders. We also clear the selection before painting, since some items may be rendered differently if they are selected. A similar QGraphicsView.

render() method can be used to render the scene (or a selected portion of it) as seen. We will omit the code for saving and loading .pgd les, since it is very similar to what we have seen before when working with binary les.

For saving, we create a QDataStream, call setVersion() on it, and write a magic number and a le version. Then we iterate over all the items in the scene, calling writeItemToStream() parameterized by the data stream and by the item for each call. For loading, we also create a QDataStream.

Then we read in the magic number and le version, and if they are correct, we delete all the existing items. As long as the le has data in it, we call readItemFromStream() parameterized by the stream. This method reads the item data and creates the items, adding them to the scene as it goes.

We have seen how the application works as a whole, and how to create and use items of two of the prede ned graphics item classes, namely, QGraphicsRectItem and QGraphicsPixmapItem. Now we will turn our attention to custom graphics view items. We will begin by looking at the TextItem subclass; this extends the QGraphicsTextItem class with additional behavior, but leaves all the drawing to the base class.

Then we will look at the BoxItem class; this class has code for both behavior and drawing.. class TextIte m(QGraphicsTextItem): def __init__(self, text, position, scene, font=QFont("Times", PointSize), matrix=QMatrix()):.
Copyright © 2DBarcode.info . All rights reserved.