首页 > 代码库 > pyQt绘图

pyQt绘图

def paintEvent(self, e):        qp = QtGui.QPainter()        qp.begin(self)        self.DrawChessBoard(qp)        self.Draw_Chess(qp)        qp.end()

pyqt的画图机制是:

  每次界面更新时,自动调用 paintEvet(self,e)函数,所以将绘图的地方重写与该函数内。首先获得画笔,然后在begin方法h和end方法之间,执行绘图方法。但是,这里有个问题,我每次界面更新都会调用该方法,但是我们并不是每次都想执行绘制的的东西。比如我这里有两个函数:绘制棋盘和绘制棋子。每次我都需要绘制棋盘,但是不是每次都需要绘制棋盘。怎么办呢?设置绘制开关。我设置一个变量,通过if 进入不同的绘制函数。

 

  补充这两个绘制函数:

技术分享
 def DrawChessBoard(self,qp):        qp.setPen(QtCore.Qt.black)        spos = [0, 0]        epos = [0, 0]        i = 0        # 画横线        while i < 15:            spos[0] = self.startpos[0]            spos[1] = self.startpos[1] + self.linelen * i            epos[0] = self.startpos[0] + self.linelen * 14            epos[1] = self.startpos[1] + self.linelen * i            # print i, spos, ‘--->‘, epos            #self.drawText(str(15 - i), spos[0] - 25, spos[1], 20)            qp.drawText(spos[0] - 35, spos[1], QString(str(15-i)))            qp.drawLine( spos[0], spos[1], epos[0], epos[1])            i += 1        i = 0        while i < 15:            spos[0] = self.startpos[0] + self.linelen * i            spos[1] = self.startpos[1]            epos[0] = self.startpos[0] + self.linelen * i            epos[1] = self.startpos[1] + self.linelen * 14            #self.drawText((self.ch[i]), epos[0], epos[1] + 20, 20)            qp.drawText(epos[0], epos[1] + 30, QString(self.ch[i]))            qp.drawLine(spos[0], spos[1], epos[0], epos[1])            i += 1
View Code

 

技术分享
def Draw_Chess(self,qp):        self.color=[QtCore.Qt.blue,QtCore.Qt.black,QtCore.Qt.white]        self.putOneChess=False        #print u‘画一个棋子在棋盘‘        for point in self.PlayArr:            arr_i,arr_j,chessKind=point            pos_x,pos_y=self.Arr_To_Pos(arr_i,arr_j)            #反走样            qp.setRenderHint(QPainter.Antialiasing, True)            #设置画笔颜色、宽度            #qp.setPen(QPen(QColor(0, 0, 0), 2))            #设置画刷颜色            qp.setBrush(self.color[chessKind])            # 绘制圆            qp.drawEllipse(QPointF(pos_x, pos_y),15, 15)        if len(self.PlayArr)>0:            point =self.PlayArr[-1]            arr_i, arr_j, chessKind = point            pos_x, pos_y = self.Arr_To_Pos(arr_i, arr_j)            # 反走样            qp.setRenderHint(QPainter.Antialiasing, True)            # 设置画笔颜色、宽度            # qp.setPen(QPen(QColor(0, 0, 0), 2))            # 设置画刷颜色            qp.setBrush(QtCore.Qt.red)            # 绘制圆            qp.drawEllipse(QPointF(pos_x, pos_y), 5, 5)        if len(self.virPlayArr)>0:            for point in self.virPlayArr:                arr_i, arr_j, chessKind = point                pos_x, pos_y = self.Arr_To_Pos(arr_i, arr_j)                # 反走样                qp.setRenderHint(QPainter.Antialiasing, True)                # 设置画笔颜色、宽度                # qp.setPen(QPen(QColor(0, 0, 0), 2))                # 设置画刷颜色                qp.setBrush(QtCore.Qt.blue)                # 绘制圆                qp.drawEllipse(QPointF(pos_x, pos_y), 15, 15)
View Code

 

pyQt绘图