{"id":699,"date":"2012-06-02T03:26:41","date_gmt":"2012-06-02T03:26:41","guid":{"rendered":"http:\/\/www.kurianos.com\/wordpress\/?p=699"},"modified":"2012-06-02T04:10:35","modified_gmt":"2012-06-02T04:10:35","slug":"sound-meter-with-pyqt","status":"publish","type":"post","link":"https:\/\/www.kurianos.com\/wordpress\/?p=699","title":{"rendered":"Sound Meter With PyQt"},"content":{"rendered":"<p>This is a simple reimplementation of QProgressBar, I am trying to build a sound meter with PyQt and I made a custom widget for meter. This is really in early dev stage. And this will be help you to know about reimplementing some stuff in a widget. <\/p>\n<p>Test Video:<\/p>\n<div class=\"lyte-wrapper fourthree\" style=\"width:420px;max-width:100%;margin:5px;\"><div class=\"lyMe\" id=\"WYL_zvQ7dI9TMIU\"><div id=\"lyte_zvQ7dI9TMIU\" data-src=\"\/\/i.ytimg.com\/vi\/zvQ7dI9TMIU\/hqdefault.jpg\" class=\"pL\"><div class=\"tC\"><div class=\"tT\"><\/div><\/div><button tabindex=\"0\" class=\"play\"><\/button><div class=\"ctrl\"><div class=\"Lctrl\"><\/div><div class=\"Rctrl\"><\/div><\/div><\/div><noscript><a href=\"https:\/\/youtu.be\/zvQ7dI9TMIU\" rel=\"nofollow\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i.ytimg.com\/vi\/zvQ7dI9TMIU\/0.jpg\" alt=\"YouTube video thumbnail\" width=\"420\" height=\"295\" \/><br \/>Watch this video on YouTube<\/a><\/noscript><\/div><\/div><div class=\"lL\" style=\"max-width:100%;width:420px;margin:5px;\"><\/div><\/p>\n<p>Here is the source code for Widget<\/p>\n<p>MQProgressBar.py<br \/>\n[Python]<br \/>\nimport os<br \/>\nfrom PyQt4 import QtGui, QtCore, uic<\/p>\n<p>class MQProgressBar(QtGui.QProgressBar):<\/p>\n<p>    def __init__(self):<br \/>\n        super(MQProgressBar, self).__init__()<br \/>\n        self.setTextVisible(False)<br \/>\n        self.__chunkOpen = False<br \/>\n        self.__heightDiff = 0<br \/>\n        self.__blockStyle  = False<\/p>\n<p>    def setChunkOn(self, mode, value = 0):<br \/>\n        if mode:<br \/>\n            self.chunk_mode = QtGui.QFrame(self)<br \/>\n            self.chunk_mode.setFrameShadow(QtGui.QFrame.Raised)<br \/>\n            self.chunk_mode.setLineWidth(2)<\/p>\n<p>            if self.orientation() == QtCore.Qt.Vertical:<br \/>\n                self.chunk_mode.setFrameShape(QtGui.QFrame.HLine)<br \/>\n            else:<br \/>\n                self.chunk_mode.setFrameShape(QtGui.QFrame.VLine)<br \/>\n            self.__chunkOpen = True<\/p>\n<p>    def setValue(self,int):<br \/>\n        get_current_val = self.value()<br \/>\n        self.updateChunk(get_current_val)<br \/>\n        QtGui.QProgressBar.setValue(self,int)<\/p>\n<p>    def updateChunk(self, value):<br \/>\n        current_x = self.height()<br \/>\n        current_y = self.width()<br \/>\n        self.__heightDiff<br \/>\n        update_val = 5<br \/>\n        if value >= 99:<br \/>\n            return<br \/>\n        elif value > 30:<br \/>\n            update_val = 10<\/p>\n<p>        if self.orientation() == QtCore.Qt.Vertical:<br \/>\n            self.chunk_mode.move(0, ((current_x &#8211; 30) &#8211; (value * current_x \/ 100  &#8211; update_val)))<br \/>\n        else:<br \/>\n            self.chunk_mode.move(((value * current_y \/ 100  &#8211; 20)) &#8211; (current_y &#8211; 80), 0)<\/p>\n<p>    def setPeakColor(self,color):<br \/>\n        self.chunk_mode.setPalette(QtGui.QPalette(color))<\/p>\n<p>    def resizeEvent(self, event):<br \/>\n        current_x = self.height()<br \/>\n        current_y = self.width()<br \/>\n        value = self.value()<br \/>\n        old_size = event.oldSize()<\/p>\n<p>        if self.orientation() == QtCore.Qt.Vertical:<br \/>\n            old_hight = old_size.height()<br \/>\n            updated_height = current_x &#8211; old_hight<br \/>\n            self.chunk_mode.move(0, current_x &#8211; 20)<br \/>\n            if value > 0:<br \/>\n                self.chunk_mode.move(0, ((current_x &#8211; 30) &#8211; (value * current_x \/ 100  &#8211; 20)))<br \/>\n        else:<br \/>\n            old_hight = old_size.width()<br \/>\n            updated_height = current_y &#8211; old_hight<br \/>\n            self.chunk_mode.move( current_y &#8211; 80 , 0)<br \/>\n            if value > 0:<br \/>\n                self.chunk_mode.move(((value * current_y \/ 100  &#8211; 20)) &#8211; (current_y &#8211; 80), 0)<\/p>\n<p>        self.__heightDiff = updated_height<br \/>\n        QtGui.QProgressBar.resizeEvent(self,event)<\/p>\n<p>    def peakVisible(self):<br \/>\n        return self.__chunkOpen<\/p>\n<p>    def setMeterColor(self, start_color = &#8220;green&#8221; , end_color = &#8220;red&#8221;):<br \/>\n        mode_val = &#8220;horizontal&#8221;<br \/>\n        if self.orientation() == QtCore.Qt.Vertical:<br \/>\n            mode_val = &#8220;vertical&#8221;<br \/>\n            if self.__blockStyle:<br \/>\n                self.setStyleSheet (&#8220;QProgressBar::chunk:%s {background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, stop: 0 %s, stop: 1 %s);margin-top: 2px; height: 10px;}&#8221; % (mode_val, end_color, end_color))<\/p>\n<p>            else:<br \/>\n                self.setStyleSheet (&#8220;QProgressBar::chunk:%s {background-color: qlineargradient(x1: 1, y1: 0, x2: 1, y2: 1, stop: 0 %s, stop: 1 %s);}&#8221; % (mode_val,end_color, start_color))<br \/>\n        else:<br \/>\n            if self.__blockStyle:<br \/>\n                self.setStyleSheet (&#8220;QProgressBar::chunk:%s {background: qlineargradient(x1: 0, y1: 0.5, x2: 1, y2: 0.5, stop: 0 %s, stop: 1 %s);margin-right: 2px; width: 10px;}&#8221; % (mode_val, end_color, end_color))<br \/>\n            else:<br \/>\n                self.setStyleSheet (&#8220;QProgressBar::chunk:%s {background-color: qlineargradient(x1: 0, y1: 1, x2: 1, y2: 1, stop: 0 %s, stop: 1 %s);}&#8221; % (mode_val,start_color, end_color))<\/p>\n<p>    def setBlockStyle(self, mode):<br \/>\n        self.__blockStyle = mode<br \/>\n        self.setMeterColor()<\/p>\n<p>    def setMeterBorder(self, color):<br \/>\n        self.setStyleSheet(&#8220;%sQProgressBar {width: 25px;border: 2px solid %s; border-radius: 8px; background: #494E58;text-align: center;padding: 0px;}&#8221; % (self.styleSheet(), color))<br \/>\n[\/Python]<\/p>\n<p>Here is the example window which has the MQProgressBar implemented.<\/p>\n<p>progress_test.py<br \/>\n[Python]<br \/>\nimport os<br \/>\nimport sys<br \/>\nimport random<\/p>\n<p>from PyQt4 import QtGui, QtCore, uic<\/p>\n<p>import MQProgressBar as MQProgressBar<\/p>\n<p>class PBRTEST(QtGui.QWidget):<br \/>\n    def __init__(self):<br \/>\n        super(PBRTEST, self).__init__()<br \/>\n        self.initUI()<\/p>\n<p>    def initUI(self):<br \/>\n        self.mypb = MQProgressBar.MQProgressBar()<br \/>\n        self.mypb.setOrientation(QtCore.Qt.Vertical)<br \/>\n        self.mypb.setChunkOn(True)<br \/>\n        self.mypb.setPeakColor(QtGui.QColor(253, 215, 42))<br \/>\n        self.mypb.setMeterColor(&#8220;green&#8221;,&#8221;yellow&#8221;)<br \/>\n        self.mypb.setMeterBorder(&#8220;black&#8221;)<\/p>\n<p>        self.timer = QtCore.QBasicTimer()<br \/>\n        self.step = 0<\/p>\n<p>        self.mypb1 = MQProgressBar.MQProgressBar()<br \/>\n        self.mypb1.setOrientation(QtCore.Qt.Vertical)<br \/>\n        self.mypb1.setChunkOn(True)<br \/>\n        self.mypb1.setPeakColor(QtGui.QColor(250, 0, 0))<br \/>\n        self.mypb1.setMeterColor(&#8220;black&#8221;,&#8221;red&#8221;)<\/p>\n<p>        self.mypb2 = MQProgressBar.MQProgressBar()<br \/>\n        self.mypb2.setOrientation(QtCore.Qt.Horizontal)<br \/>\n        self.mypb2.setChunkOn(True)<br \/>\n        self.mypb2.setPeakColor(QtGui.QColor(250, 0, 0))<br \/>\n        self.mypb2.setMeterColor(&#8220;black&#8221;,&#8221;red&#8221;)<br \/>\n        self.mypb2.setBlockStyle(True)<br \/>\n        self.mypb2.setMeterBorder(&#8220;yellow&#8221;)<\/p>\n<p>        self.mypb3 = MQProgressBar.MQProgressBar()<br \/>\n        self.mypb3.setOrientation(QtCore.Qt.Vertical)<br \/>\n        self.mypb3.setChunkOn(True)<br \/>\n        self.mypb3.setPeakColor(QtGui.QColor(0, 250, 250))<br \/>\n        self.mypb3.setMeterColor(&#8220;black&#8221;,&#8221;green&#8221;)<br \/>\n        self.mypb3.setBlockStyle(True)<br \/>\n        self.mypb3.setMeterBorder(&#8220;blue&#8221;)<\/p>\n<p>        self.mypb4 = MQProgressBar.MQProgressBar()<br \/>\n        self.mypb4.setOrientation(QtCore.Qt.Vertical)<br \/>\n        self.mypb4.setChunkOn(True)<br \/>\n        self.mypb4.setPeakColor(QtGui.QColor(0, 250, 250))<\/p>\n<p>        self.btn = QtGui.QPushButton(&#8216;Start&#8217;, self)<br \/>\n        self.btn.clicked.connect(self.doAction)<\/p>\n<p>        hbox = QtGui.QHBoxLayout()<br \/>\n        hbox.addWidget(self.mypb)<br \/>\n        hbox.addWidget(self.mypb1)<br \/>\n        hbox.addWidget(self.mypb2)<br \/>\n        hbox.addWidget(self.mypb3)<br \/>\n        hbox.addWidget(self.mypb4)<\/p>\n<p>        vbox = QtGui.QVBoxLayout()<br \/>\n        vbox.addLayout(hbox)<br \/>\n        vbox.addWidget(self.btn)<\/p>\n<p>        self.setLayout(vbox)<br \/>\n        self.setGeometry(1500, 1500, 450, 500)<br \/>\n        self.setWindowTitle(&#8216;Progress Test&#8217;)<br \/>\n        self.show()<\/p>\n<p>    def gen_rand_val(self):<br \/>\n        rand_val = random.randint(0,100)<br \/>\n        self.mypb1.setValue(rand_val)<br \/>\n        self.mypb3.setValue(rand_val)<\/p>\n<p>    def timerEvent(self, e):<\/p>\n<p>        if self.step >= 100:<br \/>\n            self.timer.stop()<br \/>\n            return<\/p>\n<p>        self.step = self.step + 1<br \/>\n        self.mypb.setValue(self.step)<br \/>\n        self.gen_rand_val()<br \/>\n        self.mypb2.setValue(self.step)<br \/>\n        self.mypb4.setValue(self.step)<\/p>\n<p>    def doAction(self):<\/p>\n<p>        if self.timer.isActive():<br \/>\n            self.timer.stop()<br \/>\n        else:<br \/>\n            self.timer.start(100, self)<\/p>\n<p>def run():<\/p>\n<p>    app = QtGui.QApplication(sys.argv)<br \/>\n    ex = PBRTEST()<br \/>\n    ex.show()<br \/>\n    sys.exit(app.exec_())<\/p>\n<p>if __name__ == &#8220;__main__&#8221;:<br \/>\n    run()<br \/>\n[\/Python]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple reimplementation of QProgressBar, I am trying to build a sound meter with PyQt and I made a custom widget for meter. This is really in early dev stage. And this will be help you to know about reimplementing some stuff in a widget. Test Video: Watch this video on YouTube Here [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[15,25,14,26],"tags":[27,30,28,29,31,32],"class_list":["post-699","post","type-post","status-publish","format-standard","hentry","category-linux","category-pyqt","category-python","category-qprogressbar","tag-pyqt-2","tag-python-2","tag-qprogressbar-2","tag-qwidget","tag-sound","tag-soundmeter"],"_links":{"self":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=699"}],"version-history":[{"count":3,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/699\/revisions"}],"predecessor-version":[{"id":701,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/699\/revisions\/701"}],"wp:attachment":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}