Spinn Code
Loading Please Wait
  • Home
  • My Profile

Share something

Explore Qt Development Topics

  • Installation and Setup
  • Core GUI Components
  • Qt Quick and QML
  • Event Handling and Signals/Slots
  • Model-View-Controller (MVC) Architecture
  • File Handling and Data Persistence
  • Multimedia and Graphics
  • Threading and Concurrency
  • Networking
  • Database and Data Management
  • Design Patterns and Architecture
  • Packaging and Deployment
  • Cross-Platform Development
  • Custom Widgets and Components
  • Qt for Mobile Development
  • Integrating Third-Party Libraries
  • Animation and Modern App Design
  • Localization and Internationalization
  • Testing and Debugging
  • Integration with Web Technologies
  • Advanced Topics

About Developer

Khamisi Kibet

Khamisi Kibet

Software Developer

I am a computer scientist, software developer, and YouTuber, as well as the developer of this website, spinncode.com. I create content to help others learn and grow in the field of software development.

If you enjoy my work, please consider supporting me on platforms like Patreon or subscribing to my YouTube channel. I am also open to job opportunities and collaborations in software development. Let's build something amazing together!

  • Email

    infor@spinncode.com
  • Location

    Nairobi, Kenya
cover picture
profile picture Bot SpinnCode

7 Months ago | 59 views

**Topic: Custom Widgets and Components - Creating a Circular Slider** In this example, we will create a custom circular slider widget using PySide6. This widget will allow users to select a value from a circular range, making it suitable for applications such as color pickers, volume controls, or angle selectors. **circular_slider.py** ```python import sys from PySide6.QtCore import Qt, QPropertyAnimation, QPoint, QRect from PySide6.QtGui import QPainter, QPen, QBrush, QColor from PySide6.QtWidgets import QWidget, QApplication class CircularSlider(QWidget): def __init__(self): super().__init__() self._angle = 0 self._minimum = 0 self._maximum = 360 self._color = QColor("blue") self._dragging = False self._last_point = QPoint() self._animation = QPropertyAnimation(self, b'angle') self._animation.setDuration(200) self._animation.setStartValue(0) @property def angle(self): return self._angle @angle.setter def angle(self, value): self._angle = value self._animation.setEndValue(value) self.update() @property def minimum(self): return self._minimum @minimum.setter def minimum(self, value): self._minimum = value self.update() @property def maximum(self): return self._maximum @maximum.setter def maximum(self, value): self._maximum = value self.update() @property def color(self): return self._color @color.setter def color(self, value): self._color = value self.update() def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(painter.Antialiasing) brush = QBrush(self._color) pen = QPen(brush, 5) painter.setPen(pen) radius = min(self.width(), self.height()) // 2 - 10 center = QPoint(self.width() // 2, self.height() // 2) start_angle = self._minimum end_angle = self._angle painter.drawPie(QRect(center.x() - radius, center.y() - radius, radius * 2, radius * 2), start_angle * 16, (end_angle - start_angle) * 16) def mousePressEvent(self, event): self._dragging = True self._last_point = event.pos() def mouseMoveEvent(self, event): if self._dragging: center = QPoint(self.width() // 2, self.height() // 2) angle = self._calculate_angle(event.pos(), center) self.angle = angle def mouseReleaseEvent(self, event): self._dragging = False def _calculate_angle(self, point, center): dx = point.x() - center.x() dy = point.y() - center.y() angle = (dy / (dx ** 2 + dy ** 2) ** 0.5) * 180 / 3.14 + 90 if dx >= 0 else (dy / (dx ** 2 + dy ** 2) ** 0.5) * 180 / 3.14 + 270 return angle if __name__ == "__main__": app = QApplication(sys.argv) slider = CircularSlider() slider.resize(200, 200) slider.angle = 120 slider.color = "red" slider.show() sys.exit(app.exec()) ``` This code creates a `CircularSlider` class that inherits from `QWidget`. It overrides the `paintEvent` method to draw the circular slider and uses the `mousePressEvent`, `mouseMoveEvent`, and `mouseReleaseEvent` methods to handle user interactions. You can use the `angle` property to set the current value of the slider, and the `minimum` and `maximum` properties to set the range of the slider. The `color` property can be used to set the color of the slider. To use this widget, simply create an instance of the `CircularSlider` class and show it. You can also use it as a custom widget in your own applications. **Example Use Case:** ```python import sys from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget from circular_slider import CircularSlider class MainWindow(QWidget): def __init__(self): super().__init__() layout = QVBoxLayout() self.setLayout(layout) self.slider = CircularSlider() self.slider.setRange(0, 360) self.slider.valueChanged.connect(self.update_label) layout.addWidget(self.slider) def update_label(self): print(f"Slider value: {self.slider.angle}") if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.resize(400, 400) window.show() sys.exit(app.exec()) ``` This example creates a `MainWindow` class that uses the `CircularSlider` widget to display a circular slider. The `update_label` method is called whenever the slider value changes, and it prints the current value of the slider.
Daily Tip

Creating a Custom Circular Slider Widget.

**Topic: Custom Widgets and Components - Creating a Circular Slider** In this example, we will create a custom circular slider widget using PySide6. This widget will allow users to select a value from a circular range, making it suitable for applications such as color pickers, volume controls, or angle selectors. **circular_slider.py** ```python import sys from PySide6.QtCore import Qt, QPropertyAnimation, QPoint, QRect from PySide6.QtGui import QPainter, QPen, QBrush, QColor from PySide6.QtWidgets import QWidget, QApplication class CircularSlider(QWidget): def __init__(self): super().__init__() self._angle = 0 self._minimum = 0 self._maximum = 360 self._color = QColor("blue") self._dragging = False self._last_point = QPoint() self._animation = QPropertyAnimation(self, b'angle') self._animation.setDuration(200) self._animation.setStartValue(0) @property def angle(self): return self._angle @angle.setter def angle(self, value): self._angle = value self._animation.setEndValue(value) self.update() @property def minimum(self): return self._minimum @minimum.setter def minimum(self, value): self._minimum = value self.update() @property def maximum(self): return self._maximum @maximum.setter def maximum(self, value): self._maximum = value self.update() @property def color(self): return self._color @color.setter def color(self, value): self._color = value self.update() def paintEvent(self, event): painter = QPainter(self) painter.setRenderHint(painter.Antialiasing) brush = QBrush(self._color) pen = QPen(brush, 5) painter.setPen(pen) radius = min(self.width(), self.height()) // 2 - 10 center = QPoint(self.width() // 2, self.height() // 2) start_angle = self._minimum end_angle = self._angle painter.drawPie(QRect(center.x() - radius, center.y() - radius, radius * 2, radius * 2), start_angle * 16, (end_angle - start_angle) * 16) def mousePressEvent(self, event): self._dragging = True self._last_point = event.pos() def mouseMoveEvent(self, event): if self._dragging: center = QPoint(self.width() // 2, self.height() // 2) angle = self._calculate_angle(event.pos(), center) self.angle = angle def mouseReleaseEvent(self, event): self._dragging = False def _calculate_angle(self, point, center): dx = point.x() - center.x() dy = point.y() - center.y() angle = (dy / (dx ** 2 + dy ** 2) ** 0.5) * 180 / 3.14 + 90 if dx >= 0 else (dy / (dx ** 2 + dy ** 2) ** 0.5) * 180 / 3.14 + 270 return angle if __name__ == "__main__": app = QApplication(sys.argv) slider = CircularSlider() slider.resize(200, 200) slider.angle = 120 slider.color = "red" slider.show() sys.exit(app.exec()) ``` This code creates a `CircularSlider` class that inherits from `QWidget`. It overrides the `paintEvent` method to draw the circular slider and uses the `mousePressEvent`, `mouseMoveEvent`, and `mouseReleaseEvent` methods to handle user interactions. You can use the `angle` property to set the current value of the slider, and the `minimum` and `maximum` properties to set the range of the slider. The `color` property can be used to set the color of the slider. To use this widget, simply create an instance of the `CircularSlider` class and show it. You can also use it as a custom widget in your own applications. **Example Use Case:** ```python import sys from PySide6.QtWidgets import QApplication, QVBoxLayout, QWidget from circular_slider import CircularSlider class MainWindow(QWidget): def __init__(self): super().__init__() layout = QVBoxLayout() self.setLayout(layout) self.slider = CircularSlider() self.slider.setRange(0, 360) self.slider.valueChanged.connect(self.update_label) layout.addWidget(self.slider) def update_label(self): print(f"Slider value: {self.slider.angle}") if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.resize(400, 400) window.show() sys.exit(app.exec()) ``` This example creates a `MainWindow` class that uses the `CircularSlider` widget to display a circular slider. The `update_label` method is called whenever the slider value changes, and it prints the current value of the slider.

Images

More from Bot

Data Classes and Sealed Classes in Kotlin
7 Months ago 51 views
Implementing CSS Animations and Transitions
7 Months ago 52 views
Creating Simple DSLs in Kotlin for Configuration and Data Handling
7 Months ago 56 views
Building a Banking System with Abstract Classes and Interfaces in C#.
7 Months ago 51 views
Flutter Hello World App Creation
7 Months ago 51 views
The Importance of Testing in Web Development.
7 Months ago 37 views
Spinn Code Team
About | Home
Contact: info@spinncode.com
Terms and Conditions | Privacy Policy | Accessibility
Help Center | FAQs | Support

© 2025 Spinn Company™. All rights reserved.
image