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

1 Year ago | 147 views

**Topic: Advanced Custom Widgets with PyQt6 and Animation** In this example, we'll create a custom animated circular progress bar using PyQt6 and Python. This widget can be used in various applications to display a progress indicator in a visually appealing way. **Prerequisites** * Python 3.8+ * PyQt6 (latest version) * pip (Python package installer) **Installation and Setup** If you haven't installed PyQt6 yet, you can do so using pip: ```bash pip install PyQt6 ``` **Custom Animated Circular Progress Bar** Here's the code for the custom circular progress bar widget: ```python import sys import math from PyQt6.QtCore import * from PyQt6.QtGui import * from PyQt6.QtWidgets import * class AnimatedCircularProgress(QProgressBar): def __init__(self): super().__init__() self._angle = 0 self._sweep_angle = 360 self.setFixedSize(100, 100) self.setStyleSheet("border: none;") self.setFormat("%p%") self.setAlignment(Qt.AlignmentFlag.AlignCenter) self.setRange(0, 100) def paintEvent(self, e): painter = QPainter(self) painter.setRenderHint(painter.RenderHint.Antialiasing) center_x = self.width() // 2 center_y = self.height() // 2 radius = min(center_x, center_y) - 10 painter.setPen(QPen(Qt.GlobalColor.black, 2)) painter.setBrush(Qt.BrushStyle.NoBrush) painter.drawEllipse(center_x - radius, center_y - radius, radius * 2, radius * 2) painter.setPen(QPen(Qt.GlobalColor.blue, 5)) painter.setBrush(Qt.BrushStyle.NoBrush) painter.save() painter.translate(center_x, center_y) painter.rotate(90) path = QPainterPath() path.arcBy(0, 0, radius, 0, self._sweep_angle) painter.drawPath(path) painter.restore() painter.setPen(QPen(Qt.GlobalColor.black, 2)) painter.drawText(QPointF(center_x, center_y), f"{self.value()}%") def set_sweep_angle(self, value): self._sweep_angle = value self.update() def animate_progress(self): if self._angle < 360: self._angle += 10 self.set_sweep_angle(self._angle) QTimer.singleShot(100, self.animate_progress) class Window(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(100, 100, 400, 300) self.setWindowTitle("Animated Circular Progress") progress_bar = AnimatedCircularProgress() progress_bar.animate_progress() layout = QVBoxLayout() layout.addWidget(progress_bar) self.setLayout(layout) if __name__ == "__main__": app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) ``` **How it works** 1. We create a custom class `AnimatedCircularProgress` inheriting from `QProgressBar`. We override the `paintEvent` method to draw the circular progress bar. 2. In the `paintEvent`, we draw two ellipses, one for the background ring and another for the progress arc. We also draw the percentage text at the center. 3. We add the `set_sweep_angle` and `animate_progress` methods to animate the progress bar. The `animate_progress` method is recursively called using `QTimer` to create the animation effect. 4. In the `Window` class, we create an instance of `AnimatedCircularProgress` and call its `animate_progress` method to start the animation. **Output** When you run the code, you'll see an animated circular progress bar with a blue arc and a percentage text at the center. **Expert Tips and Variations** * You can customize the appearance of the progress bar by modifying the colors, border, and radius in the `paintEvent` method. * To change the animation speed, you can adjust the value in `QTimer.singleShot`. * You can also add more complex animations or add support for different progress bar styles by implementing more methods in the `AnimatedCircularProgress` class. By using this custom animated circular progress bar, you can add a unique visual element to your application and create a compelling user experience.
Daily Tip

Custom Animated Circular Progress Bar with PyQt6

**Topic: Advanced Custom Widgets with PyQt6 and Animation** In this example, we'll create a custom animated circular progress bar using PyQt6 and Python. This widget can be used in various applications to display a progress indicator in a visually appealing way. **Prerequisites** * Python 3.8+ * PyQt6 (latest version) * pip (Python package installer) **Installation and Setup** If you haven't installed PyQt6 yet, you can do so using pip: ```bash pip install PyQt6 ``` **Custom Animated Circular Progress Bar** Here's the code for the custom circular progress bar widget: ```python import sys import math from PyQt6.QtCore import * from PyQt6.QtGui import * from PyQt6.QtWidgets import * class AnimatedCircularProgress(QProgressBar): def __init__(self): super().__init__() self._angle = 0 self._sweep_angle = 360 self.setFixedSize(100, 100) self.setStyleSheet("border: none;") self.setFormat("%p%") self.setAlignment(Qt.AlignmentFlag.AlignCenter) self.setRange(0, 100) def paintEvent(self, e): painter = QPainter(self) painter.setRenderHint(painter.RenderHint.Antialiasing) center_x = self.width() // 2 center_y = self.height() // 2 radius = min(center_x, center_y) - 10 painter.setPen(QPen(Qt.GlobalColor.black, 2)) painter.setBrush(Qt.BrushStyle.NoBrush) painter.drawEllipse(center_x - radius, center_y - radius, radius * 2, radius * 2) painter.setPen(QPen(Qt.GlobalColor.blue, 5)) painter.setBrush(Qt.BrushStyle.NoBrush) painter.save() painter.translate(center_x, center_y) painter.rotate(90) path = QPainterPath() path.arcBy(0, 0, radius, 0, self._sweep_angle) painter.drawPath(path) painter.restore() painter.setPen(QPen(Qt.GlobalColor.black, 2)) painter.drawText(QPointF(center_x, center_y), f"{self.value()}%") def set_sweep_angle(self, value): self._sweep_angle = value self.update() def animate_progress(self): if self._angle < 360: self._angle += 10 self.set_sweep_angle(self._angle) QTimer.singleShot(100, self.animate_progress) class Window(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(100, 100, 400, 300) self.setWindowTitle("Animated Circular Progress") progress_bar = AnimatedCircularProgress() progress_bar.animate_progress() layout = QVBoxLayout() layout.addWidget(progress_bar) self.setLayout(layout) if __name__ == "__main__": app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) ``` **How it works** 1. We create a custom class `AnimatedCircularProgress` inheriting from `QProgressBar`. We override the `paintEvent` method to draw the circular progress bar. 2. In the `paintEvent`, we draw two ellipses, one for the background ring and another for the progress arc. We also draw the percentage text at the center. 3. We add the `set_sweep_angle` and `animate_progress` methods to animate the progress bar. The `animate_progress` method is recursively called using `QTimer` to create the animation effect. 4. In the `Window` class, we create an instance of `AnimatedCircularProgress` and call its `animate_progress` method to start the animation. **Output** When you run the code, you'll see an animated circular progress bar with a blue arc and a percentage text at the center. **Expert Tips and Variations** * You can customize the appearance of the progress bar by modifying the colors, border, and radius in the `paintEvent` method. * To change the animation speed, you can adjust the value in `QTimer.singleShot`. * You can also add more complex animations or add support for different progress bar styles by implementing more methods in the `AnimatedCircularProgress` class. By using this custom animated circular progress bar, you can add a unique visual element to your application and create a compelling user experience.

Images

More from Bot

Understanding REST APIs and GraphQL
1 Year ago 117 views
Mastering Angular: Building Scalable Web Applications
11 Months ago 80 views
Lazy Loading in Angular for Better Performance
1 Year ago 104 views
Best Practices for Error Handling in Java
1 Year ago 88 views
Mastering Node.js: Building Scalable Web Applications
7 Months ago 180 views
Mastering Yii Framework: Building Scalable Web Applications
7 Months ago 77 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