python3+PyQt5图形项的自定义和交互图形项的自定义和交互 python3实现实现page
Designer应用程序应用程序
本文通过Python3+PyQt5实现《python Qt Gui 快速编程》这本书的page Designer应用程序,采用
QGraphicsView,QGraphicsScene,QGraphicsItem,这个程序包含有多个文本,图片和框的页面。有些图形类在PyQt5已过
时,所以本代码改动幅度比较大。主要的类或方法的改变如下:
QMatrix==>QTransform
setMatrix==>setTransform
rotate ==> setRotation
本例中,由于event.delta()已过时,还重写了wheelEvent方法:
def wheelEvent(self, event):
#factor = 1.41 ** (-event.delta() / 240.0)
#factor = 1.41 ** (-abs(event.startX()-event.y()) / 240.0)
factor = event.angleDelta().y()/120.0
if event.angleDelta().y()/120.0 > 0:
factor=2
else:
factor=0.5
self.scale(factor, factor)
为了保持代码可读行,增加了一个类:
class GraphicsPixmapItem(QGraphicsPixmapItem): #add by yangrongdong
def __init__(self,pixmap):
super(QGraphicsPixmapItem, self).__init__(pixmap)
本例中还有包含菜单的按钮:
if text == "&Align":
menu = QMenu(self)
for text, arg in (
("Align &Left", Qt.AlignLeft),
("Align &Right", Qt.AlignRight),
("Align &Top", Qt.AlignTop),
("Align &Bottom", Qt.AlignBottom)):
wrapper = functools.partial(self.setAlignment, arg)
self.wrapped.append(wrapper)
menu.addAction(text, wrapper)
button.setMenu(menu)
本例中还针对QStyleOptionGraphicsItem.levelOfDetail已过时,改写如下:
option.levelOfDetailFromTransform(self.transform())
下面为完整的代码:
#!/usr/bin/env python3
import functools
import random
import sys
from PyQt5.QtCore import (QByteArray, QDataStream, QFile, QFileInfo,
QIODevice, QPoint, QPointF, QRectF, Qt)
from PyQt5.QtWidgets import (QApplication, QDialog,
QDialogButtonBox, QFileDialog, QFontComboBox,
QGraphicsItem, QGraphicsPixmapItem,
QGraphicsScene, QGraphicsTextItem, QGraphicsView, QGridLayout,
QHBoxLayout, QLabel, QMenu, QMessageBox,QPushButton, QSpinBox,
QStyle, QTextEdit, QVBoxLayout)
from PyQt5.QtGui import QFont,QCursor,QFontMetrics,QTransform,QPainter,QPen,QPixmap
from PyQt5.QtPrintSupport import QPrinter,QPrintDialog
MAC = True
try:
from PyQt5.QtGui import qt_mac_set_native_menubar
except ImportError:
评论0
最新资源