Results 1 to 3 of 3

Thread: PySide2 QSlider expanding drawable area and tick label customization

  1. #1
    Join Date
    Jan 2019
    Posts
    2
    Qt products
    Qt5

    Question PySide2 QSlider expanding drawable area and tick label customization

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: PySide2 QSlider expanding drawable area and tick label customization

    If your custom drawing code needs more space than the Slider would normally allocate, you can try overriding sizeHint() and minimumSizeHint() to ask the layout for more space.

    Alternatively using a custom style of proxy style and requesting a larger area in sizeFormContents()

    Cheers,
    _

  3. #3
    Join Date
    Jan 2019
    Posts
    2
    Qt products
    Qt5

    Default Re: PySide2 QSlider expanding drawable area and tick label customization

    Quote Originally Posted by anda_skoa View Post
    If your custom drawing code needs more space than the Slider would normally allocate, you can try overriding sizeHint() and minimumSizeHint() to ask the layout for more space.

    Alternatively using a custom style of proxy style and requesting a larger area in sizeFormContents()

    Cheers,
    _
    Thanks for your answer, I solved it by using style sheets and defining custom groove lengths as can be seen in my post on stackoverflow. I will try your solution when I have the time.

Similar Threads

  1. QSlider - How to change tick color?
    By asc139 in forum Newbie
    Replies: 2
    Last Post: 20th January 2014, 14:36
  2. Replies: 2
    Last Post: 29th September 2011, 14:10
  3. qslider fixed to tick marks
    By holst in forum Qt Programming
    Replies: 4
    Last Post: 24th July 2009, 13:18
  4. QSlider with tick marks numbers
    By mastupristi in forum Qt Programming
    Replies: 3
    Last Post: 6th July 2009, 09:32
  5. QSlider with a custom set of labels for the tick marks?
    By whitefurrows in forum Qt Programming
    Replies: 3
    Last Post: 5th August 2007, 17:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.