2017-05-13 4 views
1

은 내가 setStyleSheet 여기에 의해 색상을 변경하는 방법의 예를 발견 수직으로 정렬 QProgressBarPyQt는 QProgressBar 수직 텍스트 정렬 문제

에 색상을 변경하려고 : Changing the color of a QProgressbar()

을하지만 수직 정렬을들 수 없습니다. 내 예제의 스크린 샷을 확인하십시오 :

먼저 진행 표시 줄이 두 번째 기본이고, 세 번째는 변경된 색상입니다. 하지만 텍스트가 제대로 정렬되지 않습니다 (첫 번째 기본 막대 에서처럼 필요합니다).

나는 또한 setTextDirection(QtGui.QProgressBar.TopToBottom)을 시도했지만 도움이되지 않았습니다.

그것을 밖으로 시도

코드를 알려주십시오 :

import sys 
from PyQt4 import QtGui, QtCore 

class Window(QtGui.QMainWindow): 

    def __init__(self): 
     super(Window, self).__init__() 
     self.setGeometry(50, 50, 500, 300) 

     progress1 = QtGui.QProgressBar(self) 
     progress1.setGeometry(50, 50, 25, 150) 
     progress1.setOrientation(QtCore.Qt.Vertical) 
     progress1.setMaximum(0) 
     progress1.setMaximum(10) 
     progress1.setValue(4) 
     progress1.setFormat("myServer008-Load") 

     progress2 = QtGui.QProgressBar(self) 
     progress2.setGeometry(100, 50, 25, 150) 
     # adding style will stop rotating text 90 degrees clockwise 
     progress2.setStyleSheet("QProgressBar::chunk { background-color: red; }") 
     progress2.setOrientation(QtCore.Qt.Vertical) 
     progress2.setMaximum(0) 
     progress2.setMaximum(10) 
     progress2.setValue(4) 
     progress2.setFormat("myServer008-Load") 

     progress3 = QtGui.QProgressBar(self) 
     progress3.setGeometry(150, 50, 25, 150) 
     # centring text works, but still do no rotate it 
     progress3.setStyleSheet("QProgressBar { text-align: center; } QProgressBar::chunk { background-color: red; }") 
     progress3.setOrientation(QtCore.Qt.Vertical) 
     progress3.setMaximum(0) 
     progress3.setMaximum(10) 
     progress3.setValue(4) 
     progress3.setFormat("myServer008-Load") 


app = QtGui.QApplication(sys.argv) 
GUI = Window() 
GUI.show() 
sys.exit(app.exec_()) 

답변

0

내가 자신의 수직 진행률 표시 줄 그리기와 해결 방법을 발견했다. 단순히 추가 코드를 질문 한 후

class MyBar(QtGui.QWidget): 
    """ Creates custom 'vertical progress bar'""" 

    def __init__(self, text, maximumValue, currentValue, parent=None): 
     super(MyBar, self).__init__(parent) 
     self.text = text 
     self.maximumValue = maximumValue 
     self.currentValue = currentValue 

    def setValue(self, currentValue): 
     if self.currentValue != currentValue: 
      self.currentValue = currentValue 
      self.repaint() 

    def paintEvent(self, event): 
     painter = QtGui.QPainter(self) 
     painter.translate(0, self.height() - 1) 
     painter.rotate(-90) 

     painter.setPen(QtGui.QColor(140, 138, 135)) 
     painter.drawRoundedRect(QtCore.QRectF(0, 0, self.height() - 1, self.width() - 1), 4, 4) 

     painter.setPen(QtGui.QColor(201, 199, 197)) 
     path = QtGui.QPainterPath() 
     path.addRoundedRect(QtCore.QRectF(1, 1, self.height() - 3, self.width() - 3), 3, 3) 

     painter.fillPath(path, QtGui.QColor(214, 212, 210)) 
     painter.drawPath(path) 

     path = QtGui.QPainterPath() 
     path.addRoundedRect(QtCore.QRectF(1, 1, (self.height() - 3) * self.currentValue/self.maximumValue, self.width() - 3), 3, 3) 

     painter.fillPath(path, QtGui.QColor(255, 0, 0)) 
     painter.drawPath(path) 

     painter.setPen(QtCore.Qt.black) 
     # print text centered 
     painter.drawText(5, self.width()/2 + 5, self.text) 
     painter.end() 

을 그리고 :

아마 누군가도이 사용할 수

progress4 = MyBar("myServer008-Load", 10, 4, self) 
progress4.setGeometry(200, 50, 25, 150) 

결과는 다음과 같습니다

result example