Creating Unique Custom Widgets in PyQt6
Unlock the Power of Custom Widgets in PyQt6: A Comprehensive Guide
In the realm of desktop application development, creating a unique and engaging user interface is paramount. One way to achieve this is by leveraging custom widgets in PyQt6. These widgets can be tailored to meet specific design requirements, enhancing both functionality and aesthetics.
To get started, let's create a simple custom widget that displays a circular progress bar with a percentage label at its center. This widget will demonstrate how you can combine various elements to create something truly unique.
First, we'll define our custom widget class:
import sys
from PyQt6.QtWidgets import QWidget, QLabel
from PyQt6.QtGui import QPainter, QPen, QBrush, QColor, QPalette
from PyQt6.QtCore import Qt, QRectF
class CircularProgressBar(QWidget):
def __init__(self):
super().__init__()
self._value = 0
self._minimum = 0
self._maximum = 100
self._text = ""
self._palette = QPalette()
def paintEvent(self, event):
# Initialize the painter and set up the pen and brush
painter = QPainter(self)
pen = QPen(QColor(0, 0, 0))
pen.setWidth(3)
painter.setPen(pen)
brush = QBrush(QColor(0, 0, 0))
painter.setBrush(brush)
# Calculate the center point of the widget
center = self.rect().center()
# Draw the outer circle
outer_radius = min(self.width(), self.height()) / 2 - 10
start_angle = 90 * 16
span_angle = 360 * 16
painter.drawArc(QRectF(center.x() - outer_radius, center.y() - outer_radius,
outer_radius * 2, outer_radius * 2), start_angle, span_angle)
# Draw the filled portion of the circle
inner_radius = min(self.width(), self.height()) / 2 - 10
span_angle = (self._value - self._minimum) / (self._maximum - self._minimum) * 360 * 16
painter.drawArc(QRectF(center.x() - inner_radius, center.y() - inner_radius,
inner_radius * 2, inner_radius * 2), start_angle, span_angle)
# Draw the percentage label
font = self.font()
font.setPixelSize(24)
painter.setFont(font)
painter.setRenderHint(QPainter.Antialiasing)
painter.drawText(self.rect(), Qt.AlignmentFlag.AlignCenter, f"{self._value}%")
def setValue(self, value):
self._value = value
self.update()
def setMinimum(self, minimum):
self._minimum = minimum
self.update()
def setMaximum(self, maximum):
self._maximum = maximum
self.update()
Now that we have our custom widget defined, let's integrate it into a simple application:
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt6.QtCore import Qt
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Circular Progress Bar")
self.setGeometry(100, 100, 400, 300)
layout = QVBoxLayout()
self.setLayout(layout)
circular_progress_bar = CircularProgressBar()
circular_progress_bar.setValue(75)
layout.addWidget(circular_progress_bar)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
This example demonstrates how you can create a custom widget that enhances the user interface of your application. By combining various elements and leveraging the power of QPainter, you can design unique and engaging UI components that set your application apart.
To delve deeper into the world of custom widgets and explore more advanced techniques, visit our YouTube channel at https://www.youtube.com/@SpinnTv or our website at https://www.spinncode.com. There, you'll find a wealth of resources designed to help you master the art of desktop application development with PyQt6.
Comments