I am a new Qt user and designing a controller interface. By overloading the paintEvent of the QSlider I was able to draw ticks labels and tick marks. However, the first and last of these tick labels were clipped from the top and bottom, respectively. Please see the attached screenshot for an illustration.

ss.jpg

So far I have tried setting the contents margins with setContentsMargins and changing the style which did not work at all. Here is an example code which reproduces the situation I was talking about. Is it possible to expand the drawable area around the QSlider so that the labels will not be cropped? If so how can it be done in Python?

Thanks in advance

Qt Code:
  1. import sys
  2. from PySide2.QtCore import *
  3. from PySide2.QtWidgets import *
  4. from PySide2.QtGui import *
  5.  
  6. slider_x = 150
  7. slider_y = 450
  8. slider_step = [0.01, 0.1, 1, 10, 100] # in microns
  9.  
  10.  
  11. class MySlider(QSlider):
  12. def __init__(self, type, parent=None):
  13. super(MySlider, self).__init__(parent)
  14. self.Type = type
  15.  
  16. def paintEvent(self, event):
  17. super(MySlider, self).paintEvent(event)
  18. qp = QPainter(self)
  19. pen = QPen()
  20. pen.setWidth(2)
  21. pen.setColor(Qt.red)
  22.  
  23. qp.setPen(pen)
  24. font = QFont('Times', 10)
  25. qp.setFont(font)
  26. self.setContentsMargins(50, 50, 50, 50)
  27. # size = self.size()
  28. # print(size)
  29. # print("margins", self.getContentsMargins())
  30. # print(self.getContentsMargins())
  31. # print(self.contentsRect())
  32. contents = self.contentsRect()
  33. print(contents.x())
  34. self.setFixedSize(QSize(slider_x, slider_y))
  35. max_slider = self.maximum()
  36. y_inc = 0
  37. for i in range(max_slider):
  38. qp.drawText(contents.x() - font.pointSize(), y_inc + font.pointSize() / 2, '{0:2}'.format(slider_step[i]))
  39. qp.drawLine(contents.x() + font.pointSize(), y_inc, contents.x() + contents.width(), y_inc)
  40. y_inc += slider_y/4
  41.  
  42.  
  43. class Window(QWidget):
  44. """ Inherits from QWidget """
  45. def __init__(self):
  46. super().__init__()
  47. self.title = 'Control Stages'
  48. self.left = 10
  49. self.top = 10
  50. self.width = 320
  51. self.height = 100
  52. self.AxesMapping = [0, 1, 2, 3]
  53. self.initUI()
  54.  
  55. def initUI(self):
  56. """ Initializes the GUI either using the grid layout or the absolute position layout"""
  57. self.setWindowTitle(self.title)
  58. self.setGeometry(self.left, self.top, self.width, self.height)
  59. Comp4 = self.createSlider("step_size")
  60. Comp5 = self.createSlider("speed")
  61. windowLayout = QGridLayout()
  62. windowLayout.setContentsMargins(50, 50, 50, 50)
  63. HGroupBox = QGroupBox()
  64. layout = QGridLayout()
  65. layout.addWidget(Comp4, 0, 0)
  66. layout.addWidget(Comp5, 0, 1)
  67. HGroupBox.setLayout(layout)
  68. HGroupBox.setFixedSize(QSize(740, 480))
  69. windowLayout.addWidget(HGroupBox, 0, 0)
  70. self.setLayout(windowLayout)
  71. self.show()
  72.  
  73. def createSlider(self, variant):
  74. Slider = MySlider(Qt.Vertical)
  75. Slider.Type = variant
  76. Slider.setMaximum(5)
  77. Slider.setMinimum(1)
  78. Slider.setSingleStep(1)
  79. Slider.setTickInterval(1)
  80. Slider.valueChanged.connect(lambda: self.sliderChanged(Slider))
  81. return Slider
  82.  
  83. @staticmethod
  84. def sliderChanged(Slider):
  85. print("Slider value changed to ", Slider.value(), "slider type is ", Slider.Type)
  86. if Slider.Type == "step_size":
  87. print("this is a step size slider")
  88. elif Slider.Type == "speed":
  89. print("this is a speed slider")
  90.  
  91.  
  92. if __name__ == '__main__':
  93. app = QApplication(sys.argv)
  94. ex = Window()
  95. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode