Created
September 14, 2022 14:53
-
-
Save keithel/dc8845ef46a89f5fc317db26e57adce0 to your computer and use it in GitHub Desktop.
Styling a QProgressBar with a stylesheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout | |
from PySide6.QtWidgets import QWidget, QLabel, QProgressBar, QPushButton | |
from PySide6.QtCore import Qt, Slot, QTimer | |
class Window(QMainWindow): | |
def __init__(self): | |
super().__init__() | |
self.setWindowTitle("Progress Bar Styling") | |
self.setAttribute(Qt.WA_DeleteOnClose) | |
self._progress_label = QLabel("Progress") | |
self._progress_max = 10 | |
self._progress_bar1 = QProgressBar() | |
self._progress_bar1.setMinimum(0) | |
self._progress_bar1.setMaximum(self._progress_max) | |
self._progress_bar2 = QProgressBar() | |
self._progress_bar2.setMinimum(0) | |
self._progress_bar2.setMaximum(self._progress_max) | |
self._quit_button = QPushButton("Quit") | |
self._quit_button.clicked.connect(self.close) | |
main_layout = QVBoxLayout() | |
main_layout.addWidget(self._progress_label) | |
main_layout.addWidget(self._progress_bar1) | |
main_layout.addWidget(self._progress_bar2) | |
central_widget = QWidget() | |
central_widget.setLayout(main_layout) | |
self.setCentralWidget(central_widget) | |
# Style progress bar 2 | |
stylesheet=""" | |
QProgressBar { | |
border: 2px solid grey; | |
border-radius: 5px; | |
text-align: center; | |
} | |
QProgressBar::chunk { | |
background-color: #05B8CC; | |
width: 20px; | |
margin: 0.5px; | |
}""" | |
self._progress_bar2.setStyleSheet(stylesheet) | |
self._progress_timer = QTimer() | |
self._progress_timer.timeout.connect(self.update_progress) | |
self._progress_timer.start(500) | |
@Slot() | |
def update_progress(self): | |
p1 = self._progress_bar1 | |
p2 = self._progress_bar2 | |
_max = self._progress_max | |
p1.setValue((p1.value() + 1) % (_max+1)) | |
p2.setValue((p2.value() + 1) % (_max+1)) | |
if __name__ == '__main__': | |
app = QApplication(sys.argv) | |
main_window = Window() | |
main_window.show() | |
sys.exit(app.exec()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment