def draw_needle(self): painter = QPainter(self) # painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing) painter.setRenderHint(QPainter.Antialiasing) # Koordinatenursprung in die Mitte der Flaeche legen painter.translate(self.width() / 2, self.height() / 2) painter.setPen(Qt.NoPen) painter.setBrush(self.NeedleColor) painter.rotate(((self.value - self.value_offset - self.value_min) * self.scale_angle_size / (self.value_max - self.value_min)) + 90 + self.scale_angle_start_value) painter.drawConvexPolygon(self.value_needle[0])
def _drawAttack(self, image, attacker, defender, color): painter = QPainter() painter.begin(image) painter.setRenderHint(QPainter.Antialiasing) painter.setPen(QPen(Qt.SolidLine)); painter.setBrush(QBrush(QColor(color))) x1, y1 = self.gameMap.center[attacker] x2, y2 = self.gameMap.center[defender] dx, dy = (x2 - x1, y2 - y1) l, r = math.sqrt(dx * dx + dy * dy), 8 dx, dy = dy / l, -dx / l x11, y11 = x1 + dx * r, y1 + dy * r x12, y12 = x1 - dx * r, y1 - dy * r painter.drawConvexPolygon(QPoint(x11, y11), QPoint(x12, y12), QPoint(x2, y2)) painter.end()
def draw_needle(self): painter = QPainter(self) # painter.setRenderHint(QtGui.QPainter.HighQualityAntialiasing) painter.setRenderHint(QPainter.Antialiasing) # Koordinatenursprung in die Mitte der Flaeche legen painter.translate(self.width() / 2, self.height() / 2) painter.setPen(Qt.NoPen) painter.setBrush(self.NeedleColor) # painter.rotate(((self.value - self.value_offset - self.value_min) * self.scale_angle_size / # (self.value_max - self.value_min)) + 90 + self.scale_angle_start_value) frac, whole = math.modf(self.value) if frac >= 0: frac = 1-frac rotFraction = ((self.value - self.value_offset - self.value_min)/(self.value_max - self.value_min)) else: frac = 1+frac rotFraction = (((whole-frac) - self.value_offset - self.value_min)/(self.value_max - self.value_min)) painter.rotate((1-rotFraction) * self.scale_angle_size + 90 + self.scale_angle_start_value) painter.drawConvexPolygon(self.value_needle[0])
def paintEvent(self, e): time = QTime.currentTime() qp = QPainter() qp.begin(self) #qp.setRenderHint(QPainter.Antialiasing) # 开启这个抗锯齿,会很占cpu的! qp.translate(self.width() / 2, self.height() / 2) qp.scale(self.side / 200.0, self.side / 200.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.hourColor) qp.save() qp.rotate(30.0 * ((time.hour() + time.minute()/ 60.0))) qp.drawConvexPolygon(self.hourHand) qp.restore() qp.setPen(self.hourColor) for i in range(12): qp.drawLine(88, 0, 96, 0) qp.rotate(30.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.minuteColor) qp.save() qp.rotate(6.0 * ((time.minute() + (time.second()+time.msec()/1000.0) / 60.0))) qp.drawConvexPolygon(self.minuteHand) qp.restore() qp.setPen(self.minuteColor) for i in range(60): if (i % 5) is not 0: qp.drawLine(92, 0, 96, 0) qp.rotate(6.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.secondColor) qp.save() qp.rotate(6.0*(time.second()+time.msec()/1000.0)) qp.drawConvexPolygon(self.secondHand) qp.restore() qp.end()
def paintEvent(self, e): time = QTime.currentTime() qp = QPainter() qp.begin(self) #qp.setRenderHint(QPainter.Antialiasing) # 开启这个抗锯齿,会很占cpu的! qp.translate(self.width() / 2, self.height() / 2) qp.scale(self.side / 200.0, self.side / 200.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.hourColor) qp.save() qp.rotate(30.0 * ((time.hour() + time.minute() / 60.0))) qp.drawConvexPolygon(self.hourHand) qp.restore() qp.setPen(self.hourColor) for i in range(12): qp.drawLine(88, 0, 96, 0) qp.rotate(30.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.minuteColor) qp.save() qp.rotate(6.0 * ((time.minute() + (time.second() + time.msec() / 1000.0) / 60.0))) qp.drawConvexPolygon(self.minuteHand) qp.restore() qp.setPen(self.minuteColor) for i in range(60): if (i % 5) is not 0: qp.drawLine(92, 0, 96, 0) qp.rotate(6.0) qp.setPen(QtCore.Qt.NoPen) qp.setBrush(self.secondColor) qp.save() qp.rotate(6.0 * (time.second() + time.msec() / 1000.0)) qp.drawConvexPolygon(self.secondHand) qp.restore() qp.end()
class Meter(QtGui.QWidget): """ a PyQt instance of QtMeter from Qt example code """ def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.value = 0 self.minValue = 0 self.maxValue = 100 self.logo = "" self.scaleMajor = 10 self.scaleMijor = 10 self.startAngle = 60 self.endAngle = 60 self.crownColor = Qt.blue self.foreground = Qt.green self.background = Qt.black self.timer = QTimer() self.timer.timeout.connect(self.update) self.timer.start(200) self.resize(200, 200) def updateValue(self): pass def paintEvent(self, QPaintEvent): self.updateValue() self.side = min(self.width(), self.height()) self.painter = QPainter() self.painter.begin(self) #self.painter.setRenderHint(QPainter.Antialiasing) self.painter.translate(self.width() / 2, self.height() / 2) self.painter.scale(self.side / 200.0, self.side / 200.0) self.painter.setPen(Qt.NoPen) self.drawCrown() self.drawBackgroud() self.drawLogo() self.drawScale() self.drawScaleNum() self.drawNumbericValue() self.drawPointer() self.painter.end() def setValue(self, updatefun): self.value = updatefun() def setLogo(self, logo): self.logo = logo def drawCrown(self): self.painter.save() self.painter.setPen(QtGui.QPen(self.crownColor, 3)) self.painter.drawEllipse(-92, -92, 184, 184) self.painter.restore() def drawBackgroud(self): self.painter.save() self.painter.setBrush(self.background) self.painter.drawEllipse(-92, -92, 184, 184) self.painter.restore() def drawScale(self): self.painter.save() self.painter.rotate(self.startAngle) self.painter.setPen(self.foreground) steps = self.scaleMajor * self.scaleMijor angleStep = (360.0 - self.startAngle - self.endAngle) / steps pen = QtGui.QPen(self.painter.pen()) for i in xrange(steps + 1): if i % self.scaleMajor == 0: pen.setWidth(1) self.painter.setPen(pen) self.painter.drawLine(0, 62, 0, 72) else: pen.setWidth(0) self.painter.setPen(pen) self.painter.drawLine(0, 62, 0, 65) self.painter.rotate(angleStep) self.painter.restore() def drawScaleNum(self): self.painter.save() self.painter.setPen(self.foreground) startRad = (360 - self.startAngle - 90) * (3.14 / 180) deltaRad = (360 - self.startAngle - self.endAngle) * (3.14 / 180) / self.scaleMajor fm = QtGui.QFontMetricsF(self.font()) for i in xrange(self.scaleMajor + 1): sina = sin(startRad - i * deltaRad) cosa = cos(startRad - i * deltaRad) tmpVal = 1.0 * i * ((self.maxValue - self.minValue) / self.scaleMajor) + self.minValue numstr = QString("%1").arg(tmpVal) w = fm.size(Qt.TextSingleLine, numstr).width() h = fm.size(Qt.TextSingleLine, numstr).height() x = 82 * cosa - w / 2 y = -82 * sina + h / 4 self.painter.drawText(x, y, numstr) self.painter.restore() def drawLogo(self): self.painter.save() self.painter.setPen(self.foreground) self.painter.setBrush(self.foreground) logostr = QString(self.logo) fm = QtGui.QFontMetricsF(self.font()) w = fm.size(Qt.TextSingleLine, logostr).width() self.painter.drawText(-w / 2, -30, logostr) self.painter.restore() def drawNumbericValue(self): self.painter.save() color = QtGui.QColor(150, 150, 200) pen = self.painter.pen() pen.setWidth(3) self.painter.setPen(pen) self.painter.setPen(color) self.painter.drawRect(-30, 30, 60, 14) cpustr = QString("%1").arg(self.value) fm = QtGui.QFontMetricsF(self.font()) w = fm.size(Qt.TextSingleLine, cpustr).width() self.painter.setPen(self.foreground) self.painter.drawText(-w / 2, 42, cpustr) self.painter.restore() def drawPointer(self): self.painter.save() self.pointerHand = QPolygon([-2, 0, 2, 0, 0, 60]) self.pointerColor = QColor(127, 0, 127) self.painter.setBrush(self.pointerColor) self.painter.rotate(self.startAngle) degRotate = (360.0 - self.startAngle - self.endAngle) / ( self.maxValue - self.minValue) * (self.value - self.minValue) self.painter.rotate(degRotate) self.painter.drawConvexPolygon(self.pointerHand) self.painter.restore()
def paintEvent(self, event): """Adapted from http://doc.qt.io/qt-5/qtwidgets-widgets-analogclock-example.html""" HOURHAND = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -55)]) MINUTEHAND = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -87)]) HOURCOLOR = QColor(Qt.black) MINUTECOLOR = QColor(0x11, 0x11, 0x11, 0xAA) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) SIDE = 200 side = min(self.width(), self.height()) painter.scale(side / SIDE, side / SIDE) # Background (night/day) if self._time_to is not None: time = self._time_to.time() hour_offset = time.hour() + time.minute() / 60 DAY, NIGHT = QColor(Qt.white), QColor('#5555ff') if 7 <= hour_offset <= 19: background = DAY elif 6 <= hour_offset <= 7: palette = GradientPaletteGenerator(NIGHT, DAY) background = palette[(hour_offset - 6) / (7 - 6)] elif 19 <= hour_offset <= 20: palette = GradientPaletteGenerator(DAY, NIGHT) background = palette[(hour_offset - 19) / (20 - 19)] else: assert hour_offset < 7 or hour_offset > 20 background = NIGHT painter.setBrush(QBrush(background)) painter.setPen(HOURCOLOR) painter.drawEllipse(-SIDE / 2, -SIDE / 2, SIDE, SIDE) # Minute tickmarks painter.save() painter.setPen(MINUTECOLOR) for j in range(60): painter.drawLine(94, 0, 97, 0) painter.rotate(6) painter.restore() # Hour tickmarks painter.save() painter.setPen(HOURCOLOR) for _ in range(12): painter.drawLine(88, 0, 98, 0) painter.rotate(30) painter.restore() # Hour span if self._time_from is not None: time_from = self._time_from.time() time_to = self._time_to.time() if time_from.secsTo( time_to) / 3600 > .2: # Don't draw really small intervals hour_from = (time_from.hour() + time_from.minute() / 60) % 12 - 3 hour_to = (time_to.hour() + time_to.minute() / 60) % 12 - 3 startAngle = -hour_to * 30 * 16 spanAngle = -hour_from * 30 * 16 - startAngle color = QColor(0xFF, 0xFF, 0, 0xAA) painter.save() painter.setBrush(QBrush(color, Qt.DiagCrossPattern)) painter.setPen(color.darker(180)) painter.drawPie(-SIDE / 2, -SIDE / 2, SIDE, SIDE, startAngle, spanAngle) painter.restore() # Hour and minute hand if self._time_to is not None: time = self._time_to.time() painter.setPen(Qt.NoPen) painter.save() painter.setBrush(HOURCOLOR) painter.rotate(30 * (time.hour() + time.minute() / 60)) painter.drawConvexPolygon(HOURHAND) painter.restore() painter.save() painter.setBrush(MINUTECOLOR) painter.rotate(6 * (time.minute() + time.second() / 60)) painter.drawConvexPolygon(MINUTEHAND) painter.restore()
def paintEvent(self, event): """Adapted from http://doc.qt.io/qt-5/qtwidgets-widgets-analogclock-example.html""" HOURHAND = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -55)]) MINUTEHAND = QPolygon([QPoint(7, 8), QPoint(-7, 8), QPoint(0, -87)]) HOURCOLOR = QColor(Qt.black) MINUTECOLOR = QColor(0x11, 0x11, 0x11, 0xAA) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing) painter.translate(self.width() / 2, self.height() / 2) SIDE = 200 side = min(self.width(), self.height()) painter.scale(side / SIDE, side / SIDE) # Background (night/day) if self._time_to is not None: time = self._time_to.time() hour_offset = time.hour() + time.minute() / 60 DAY, NIGHT = QColor(Qt.white), QColor('#5555ff') if 7 <= hour_offset <= 19: background = DAY elif 6 <= hour_offset <= 7: palette = GradientPaletteGenerator(NIGHT, DAY) background = palette[(hour_offset - 6) / (7 - 6)] elif 19 <= hour_offset <= 20: palette = GradientPaletteGenerator(DAY, NIGHT) background = palette[(hour_offset - 19) / (20 - 19)] else: assert hour_offset < 7 or hour_offset > 20 background = NIGHT painter.setBrush(QBrush(background)) painter.setPen(HOURCOLOR) painter.drawEllipse(-SIDE / 2, -SIDE / 2, SIDE, SIDE) # Minute tickmarks painter.save() painter.setPen(MINUTECOLOR) for j in range(60): painter.drawLine(94, 0, 97, 0) painter.rotate(6) painter.restore() # Hour tickmarks painter.save() painter.setPen(HOURCOLOR) for _ in range(12): painter.drawLine(88, 0, 98, 0) painter.rotate(30) painter.restore() # Hour span if self._time_from is not None: time_from = self._time_from.time() time_to = self._time_to.time() if time_from.secsTo(time_to) / 3600 > .2: # Don't draw really small intervals hour_from = (time_from.hour() + time_from.minute() / 60) % 12 - 3 hour_to = (time_to.hour() + time_to.minute() / 60) % 12 - 3 startAngle = -hour_to * 30 * 16 spanAngle = -hour_from * 30 * 16 - startAngle color = QColor(0xFF, 0xFF, 0, 0xAA) painter.save() painter.setBrush(QBrush(color, Qt.DiagCrossPattern)) painter.setPen(color.darker(180)) painter.drawPie(-SIDE / 2, -SIDE / 2, SIDE, SIDE, startAngle, spanAngle) painter.restore() # Hour and minute hand if self._time_to is not None: time = self._time_to.time() painter.setPen(Qt.NoPen) painter.save() painter.setBrush(HOURCOLOR) painter.rotate(30 * (time.hour() + time.minute() / 60)) painter.drawConvexPolygon(HOURHAND) painter.restore() painter.save() painter.setBrush(MINUTECOLOR) painter.rotate(6 * (time.minute() + time.second() / 60)) painter.drawConvexPolygon(MINUTEHAND) painter.restore()
class Meter(QtGui.QWidget): """ a PyQt instance of QtMeter from Qt example code """ def __init__(self,parent=None): QtGui.QWidget.__init__(self,parent) self.value = 0 self.minValue = 0 self.maxValue = 100 self.logo = "" self.scaleMajor = 10 self.scaleMijor = 10 self.startAngle = 60 self.endAngle = 60 self.crownColor = Qt.blue self.foreground = Qt.green self.background = Qt.black self.timer = QTimer() self.timer.timeout.connect(self.update) self.timer.start(200) self.resize(200,200) def updateValue(self): pass def paintEvent(self,QPaintEvent): self.updateValue() self.side = min(self.width(),self.height()) self.painter = QPainter() self.painter.begin(self) #self.painter.setRenderHint(QPainter.Antialiasing) self.painter.translate(self.width()/2,self.height()/2) self.painter.scale(self.side / 200.0, self.side / 200.0) self.painter.setPen(Qt.NoPen) self.drawCrown() self.drawBackgroud() self.drawLogo() self.drawScale() self.drawScaleNum() self.drawNumbericValue() self.drawPointer() self.painter.end() def setValue(self,updatefun): self.value = updatefun() def setLogo(self,logo): self.logo = logo def drawCrown(self): self.painter.save() self.painter.setPen(QtGui.QPen(self.crownColor, 3)) self.painter.drawEllipse(-92, -92, 184, 184) self.painter.restore() def drawBackgroud(self): self.painter.save() self.painter.setBrush(self.background) self.painter.drawEllipse(-92, -92, 184, 184) self.painter.restore() def drawScale(self): self.painter.save() self.painter.rotate(self.startAngle) self.painter.setPen(self.foreground) steps = self.scaleMajor * self.scaleMijor angleStep = (360.0 - self.startAngle - self.endAngle) /steps pen = QtGui.QPen(self.painter.pen()) for i in xrange(steps+1): if i % self.scaleMajor == 0: pen.setWidth(1) self.painter.setPen(pen) self.painter.drawLine(0, 62, 0, 72) else: pen.setWidth(0) self.painter.setPen(pen) self.painter.drawLine(0, 62, 0, 65) self.painter.rotate(angleStep) self.painter.restore() def drawScaleNum(self): self.painter.save() self.painter.setPen(self.foreground) startRad = (360 - self.startAngle - 90) * (3.14 / 180) deltaRad = (360 - self.startAngle - self.endAngle) * (3.14 / 180) / self.scaleMajor fm = QtGui.QFontMetricsF(self.font()) for i in xrange(self.scaleMajor+1): sina = sin(startRad - i * deltaRad) cosa = cos(startRad - i * deltaRad) tmpVal = 1.0 * i *((self.maxValue - self.minValue) / self.scaleMajor) + self.minValue numstr = QString( "%1" ).arg(tmpVal) w = fm.size(Qt.TextSingleLine,numstr).width() h = fm.size(Qt.TextSingleLine,numstr).height() x = 82 * cosa - w / 2 y = -82 * sina + h / 4 self.painter.drawText(x, y, numstr) self.painter.restore() def drawLogo(self): self.painter.save() self.painter.setPen(self.foreground) self.painter.setBrush(self.foreground) logostr = QString(self.logo) fm = QtGui.QFontMetricsF(self.font()) w = fm.size(Qt.TextSingleLine,logostr).width() self.painter.drawText(-w / 2, -30, logostr) self.painter.restore() def drawNumbericValue(self): self.painter.save() color = QtGui.QColor(150, 150, 200) pen = self.painter.pen() pen.setWidth(3) self.painter.setPen(pen) self.painter.setPen(color) self.painter.drawRect(-30, 30, 60, 14) cpustr = QString("%1").arg(self.value) fm = QtGui.QFontMetricsF(self.font()) w = fm.size(Qt.TextSingleLine,cpustr).width() self.painter.setPen(self.foreground) self.painter.drawText(-w / 2, 42, cpustr) self.painter.restore() def drawPointer(self): self.painter.save() self.pointerHand=QPolygon([-2,0, 2,0, 0,60]) self.pointerColor = QColor(127 , 0, 127) self.painter.setBrush(self.pointerColor) self.painter.rotate(self.startAngle) degRotate = (360.0 - self.startAngle - self.endAngle)/(self.maxValue - self.minValue)*(self.value - self.minValue) self.painter.rotate(degRotate) self.painter.drawConvexPolygon(self.pointerHand) self.painter.restore()