【PyQt4实例7】QPainter坐标系的变换【转载】

转载:地址:http://blog.csdn.net/ly375159507/

#-*- coding:utf-8 -*-

from PyQt4.QtCore import *
from PyQt4.QtGui import *

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf-8"))

class PainArea(QWidget):
    def __init__(self):
        super(PainArea,self).__init__()
        self.setPalette(QPalette(Qt.white))
        self.setAutoFillBackground(True)
        self.setMinimumSize(400,400)

        self.scale = 1
        self.angle = 0
        self.translate =0
        self.shear = 0
    def setScale(self,x):
        self.scale = x/5.0
        self.update()

    def setTranslate(self,x):
        self.translate = x
        self.update()

    def setRotate(self,x):
        self.angle = x
        self.update()

    def setShear(self,x):
        self.shear = (x-10.0)/10.0
        self.update()
    def paintEvent(self,e):
        p = QPainter(self)
        p.translate(200,200)

        path = QPainterPath()
        path.addRect(-100,-50,200,100)
        p.rotate(self.angle)
        p.scale(self.scale,self.scale)
        p.translate(self.translate,self.translate)
        p.shear(self.shear,self.shear)
        p.setPen(Qt.red)
        p.drawLine(0,0,150,0)
        p.drawLine(148,-2,150,0)
        p.drawLine(148,2,150,0)
        p.drawText(150,2,"X")
        p.drawLine(0,0,0,150)
        p.drawLine(-2,148,0,150)
        p.drawLine(2,148,0,150)
        p.drawText(2,150,"Y")
        p.setPen(Qt.blue)
        p.drawPath(path)


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow,self).__init__()
        self.setWindowTitle(self.tr("QPainter坐标系的变换"))
        self.area = PainArea()
        self.createwindow()
    def slotRotate(self,x):
        self.area.setRotate(x)

    def slotScale(self,x):
        self.area.setScale(x)

    def slotTranslate(self,x):
        self.area.setTranslate(x)

    def slotShear(self,x):
        self.area.setShear(x)

    def createwindow(self):
        self.rotatetext = QLabel(self.tr("旋转"))
        self.rotatetspinbox = QSpinBox(self)
        self.rotatetspinbox.setRange(1,10)
        self.rotatetspinbox.valueChanged.connect(self.slotRotate)

        self.scaletext = QLabel(self.tr("缩放"))
        self.scalespinbox = QSpinBox(self)
        self.scalespinbox.setRange(1,10)
        self.scalespinbox.valueChanged.connect(self.slotScale)

        self.translatetext = QLabel(self.tr("平移"))
        self.translatespinbox = QSpinBox(self)
        self.translatespinbox.setRange(1,10)
        self.translatespinbox.valueChanged.connect(self.slotTranslate)


        self.sheartext = QLabel(self.tr("切变"))
        self.shearspinbox = QSpinBox(self)
        self.shearspinbox.setRange(1,10)
        self.shearspinbox.valueChanged.connect(self.slotShear)

        self.rightlayout = QVBoxLayout()
        self.rightlayout.addWidget(self.rotatetext)
        self.rightlayout.addWidget(self.rotatetspinbox)
        self.rightlayout.addWidget(self.scaletext)
        self.rightlayout.addWidget(self.scalespinbox)
        self.rightlayout.addWidget(self.translatetext)
        self.rightlayout.addWidget(self.translatespinbox)
        self.rightlayout.addWidget(self.sheartext)
        self.rightlayout.addWidget(self.shearspinbox)

        self.leftlayout = QHBoxLayout()
        self.leftlayout.addWidget(self.area)

        self.mainlayout = QHBoxLayout()
        self.mainlayout.addLayout(self.leftlayout)
        self.mainlayout.addLayout(self.rightlayout)
        self.setLayout(self.mainlayout)



if __name__ == '__main__':
    import  sys
    app = QApplication(sys.argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())




你可能感兴趣的:(python)