Created
April 19, 2016 19:41
-
-
Save EricTRocks/2aa65a50f346ad65ec264da189ad0d03 to your computer and use it in GitHub Desktop.
QSlider with Text showing Value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PySide import QtCore, QtGui, QtOpenGL | |
class SliderWithValue(QtGui.QSlider): | |
def __init__(self, parent=None): | |
super(SliderWithValue, self).__init__(parent) | |
self.stylesheet = """ | |
QSlider::groove:vertical { | |
background-color: #222; | |
width: 30px; | |
} | |
QSlider::handle:vertical { | |
border: 1px #438f99; | |
border-style: outset; | |
margin: -2px 0; | |
width: 30px; | |
height: 3px; | |
background-color: #438f99; | |
} | |
QSlider::sub-page:vertical { | |
background: #4B4B4B; | |
} | |
QSlider::groove:horizontal { | |
background-color: #222; | |
height: 30px; | |
} | |
QSlider::handle:horizontal { | |
border: 1px #438f99; | |
border-style: outset; | |
margin: -2px 0; | |
width: 3px; | |
height: 30px; | |
background-color: #438f99; | |
} | |
QSlider::sub-page:horizontal { | |
background: #4B4B4B; | |
} | |
""" | |
# self.setStyleSheet(self.stylesheet) | |
def paintEvent(self, event): | |
QtGui.QSlider.paintEvent(self, event) | |
curr_value = str(self.value() / 1000.00) | |
round_value = round(float(curr_value), 2) | |
painter = QtGui.QPainter(self) | |
painter.setPen(QtGui.QPen(QtCore.Qt.white)) | |
font_metrics = QtGui.QFontMetrics(self.font()) | |
font_width = font_metrics.boundingRect(str(round_value)).width() | |
font_height = font_metrics.boundingRect(str(round_value)).height() | |
rect = self.geometry() | |
if self.orientation() == QtCore.Qt.Horizontal: | |
horizontal_x_pos = rect.width() - font_width - 5 | |
horizontal_y_pos = rect.height() * 0.75 | |
painter.drawText(QtCore.QPoint(horizontal_x_pos, horizontal_y_pos), str(round_value)) | |
elif self.orientation() == QtCore.Qt.Vertical: | |
vertical_x_pos = rect.width() - font_width - 5 | |
vertical_y_pos = rect.height() * 0.75 | |
painter.drawText(QtCore.QPoint(rect.width() / 2.0 - font_width / 2.0, rect.height() - 5), str(round_value)) | |
else: | |
pass | |
painter.drawRect(rect) | |
if __name__ == '__main__': | |
app = QtGui.QApplication([]) | |
win = QtGui.QWidget() | |
win.setWindowTitle('Test Slider with Text') | |
win.setMinimumSize(600, 400) | |
layout = QtGui.QVBoxLayout() | |
win.setLayout(layout) | |
sliderWithValue = SliderWithValue(QtCore.Qt.Horizontal) | |
sliderWithValue.setMinimum(0.0) | |
sliderWithValue.setMaximum(10 * 1000) | |
sliderWithValue.setTickInterval(1000) | |
sliderWithValue.setSingleStep(500) | |
sliderWithValue.setPageStep(1000) | |
sliderWithValue.setTickPosition(QtGui.QSlider.TicksBelow) | |
sliderWithValue.setSizePolicy(QtGui.QSizePolicy.MinimumExpanding, QtGui.QSizePolicy.Fixed) | |
sliderWithValue.setValue(1 * 1000) | |
layout.addWidget(sliderWithValue) | |
win.show() | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent, I found what I was looking