python - PyQt Rounded progress bar -
inspired post , code here any pyqt circular progress bar? trying embed 4 rounded progress bar in gui application. in gui not need border of progress bar , base rectangle. how can remove these 2 in code of paintevent.
def paintevent(self, event): outerradius = min(125,110) baserect = qtcore.qrectf(1, 1, outerradius-2, outerradius-2) # don't want rectangular shape behind circular bar buffer = qtgui.qimage(outerradius, outerradius, qtgui.qimage.format_argb32) buffer.fill(255) p = qtgui.qpainter(buffer) p.setrenderhint(qtgui.qpainter.antialiasing) # data brush self.rebuilddatabrushifneeded() # background self.drawbackground(p, buffer.rect()) # base circle self.drawbase(p, baserect) # data circle arcstep = 360.0 / (self.max - self.min) * self.value self.drawvalue(p, baserect, self.value, arcstep) # center circle innerrect, innerradius = self.calculateinnerrect(baserect, outerradius) self.drawinnerbackground(p, innerrect) # text self.drawtext(p, innerrect, innerradius, self.value) # draw bar p.end() painter = qtgui.qpainter(self) painter.drawimage(0, 0, buffer) def drawbackground(self, p, baserect): p.fillrect(baserect, self.palette().background()) def drawbase(self, p, baserect): bs = self.barstyle if bs == self.styledonut: p.setpen(qtgui.qpen(self.palette().shadow().color(), self.outlinepenwidth)) p.setbrush(self.palette().base()) p.drawarc(baserect,1,116) elif bs == self.stylepie: p.setpen(qtgui.qpen(self.palette().base().color(), self.outlinepenwidth)) p.setbrush(self.palette().base()) p.drawarc(baserect,1,116) elif bs == self.styleline: p.setpen(qtgui.qpen(self.palette().base().color(), self.outlinepenwidth)) p.setbrush(qt.qt.nobrush) p.drawarc(baserect.adjusted(self.outlinepenwidth/2, self.outlinepenwidth/2, -self.outlinepenwidth/2, -self.outlinepenwidth/2)) def drawvalue(self, p, baserect, value, arclength): # nothing draw if value == self.min: return # line style if self.barstyle == self.styleline: p.setpen(qtgui.qpen(self.palette().highlight().color(), self.datapenwidth)) p.setbrush(qt.qt.nobrush) p.drawarc(baserect.adjusted(self.outlinepenwidth/2, self.outlinepenwidth/2, -self.outlinepenwidth/2, -self.outlinepenwidth/2), self.nullposition * 16, -arclength * 16) return # pie , donut styles datapath = qtgui.qpainterpath() datapath.setfillrule(qt.qt.windingfill) # pie segment outer datapath.moveto(baserect.center()) datapath.arcto(baserect, self.nullposition, -arclength) datapath.lineto(baserect.center()) p.setbrush(self.palette().highlight()) p.setpen(qtgui.qpen(self.palette().shadow().color(), self.datapenwidth)) p.drawpath(datapath) def calculateinnerrect(self, baserect, outerradius): # line style if self.barstyle == self.styleline: innerradius = outerradius - self.outlinepenwidth else: # pie , donut styles innerradius = outerradius * self.donutthicknessratio delta = (outerradius - innerradius) / 2. innerrect = qtcore.qrectf(delta, delta, innerradius, innerradius) return innerrect, innerradius def drawinnerbackground(self, p, innerrect): if self.barstyle == self.styledonut: p.setbrush(self.palette().alternatebase()) cmod = p.compositionmode() p.setcompositionmode(qtgui.qpainter.compositionmode_source) p.drawellipse(innerrect) p.setcompositionmode(cmod) def drawtext(self, p, innerrect, innerradius, value): if not self.format: return text = self.valuetotext(value) # !!! revise f = self.font() # f.setpixelsize(innerradius * max(0.05, (0.35 - self.decimals * 0.08))) f.setpixelsize(innerradius * 1.8 / len(text)) p.setfont(f) textrect = innerrect p.setpen(self.palette().text().color()) p.drawtext(textrect, qt.qt.aligncenter, text) def valuetotext(self, value): texttodraw = self.format format_string = '{' + ':.{}f'.format(self.decimals) + '}' if self.updateflags & self.uf_value: texttodraw = texttodraw.replace("%v", format_string.format(value)) if self.updateflags & self.uf_percent: percent = (value - self.min) / (self.max - self.min) * 100.0 texttodraw = texttodraw.replace("%p", format_string.format(percent)) if self.updateflags & self.uf_max: m = self.max - self.min + 1 texttodraw = texttodraw.replace("%m", format_string.format(m)) return texttodraw def valueformatchanged(self): self.updateflags = 0; if "%v" in self.format: self.updateflags |= self.uf_value if "%p" in self.format: self.updateflags |= self.uf_percent if "%m" in self.format: self.updateflags |= self.uf_max self.update() def rebuilddatabrushifneeded(self): if self.rebuildbrush: self.rebuildbrush = false databrush = qtgui.qconicalgradient() databrush.setcenter(0.5,0.5) databrush.setcoordinatemode(qtgui.qgradient.stretchtodevicemode) pos, color in self.gradientdata: databrush.setcolorat(1.0 - pos, color) # angle databrush.setangle(self.nullposition) p = self.palette() p.setbrush(qtgui.qpalette.highlight, databrush) self.setpalette(p) class tstwidget(qtgui.qwidget): def __init__(self): super(type(self), self).__init__() self.bar = qroundprogressbar() self.bar.setfixedsize(400, 400) #self.bar.setdatapenwidth(3) #self.bar.setoutlinepenwidth(3) self.bar.setdonutthicknessratio(0.85) self.bar.setdecimals(1) self.bar.setformat('%p %') # self.bar.resetformat() self.bar.setnullposition(180) self.bar.setbarstyle(qroundprogressbar.styledonut) self.bar.setdatacolors([(0., qtgui.qcolor.fromrgb(0,159,80)), (0.5, qtgui.qcolor.fromrgb(255,255,0)), (1., qtgui.qcolor.fromrgb(255,255,0))]) self.bar.setrange(0, 100) self.bar.setvalue(85) lay = qtgui.qvboxlayout() lay.addwidget(self.bar) self.setlayout(lay) def main(): app = qtgui.qapplication(sys.argv) ex = tstwidget() ex.show() app.exec_() if __name__ == '__main__': main()
Comments
Post a Comment