Search code examples
pythonqtpyqtpyqt5statusbar

PyQt5: How to align the elements of the status bar?


I've created a status bar which looks like this:

def initStatusbar(self):
    self.zoomSlider = QSlider(Qt.Horizontal)
    self.zoomSlider.setMaximumWidth(200)
    self.zoomSlider.setRange(1, 200)
    self.zoomSlider.setSingleStep(10)
    self.zoomSlider.setValue(100)

    self.progressbar = QProgressBar()
    self.progressbar.setMaximumWidth(400)
    self.statusBar().addWidget(self.progressbar)
    self.statusBar().addWidget(self.zoomSlider)

Screenshot is below:

enter image description here

But I want to replace the progress bar and the slider like in that screenshot:

enter image description here


Solution

  • An initial solution would be to use a stretch greater than zero, but this would make the QProgressBar stretch without limits, in this case it is best to include it inside another widget and embed this in the QStatusBar.

    def initStatusbar(self):
        self.zoomSlider = QSlider(Qt.Horizontal)
        self.zoomSlider.setMaximumWidth(200)
        self.zoomSlider.setRange(1, 200)
        self.zoomSlider.setSingleStep(10)
        self.zoomSlider.setValue(100)
    
        self.progressbar = QProgressBar()
        self.progressbar.setMaximumWidth(400)
    
        widget = QWidget(self)
        widget.setLayout(QHBoxLayout())
        widget.layout().addWidget(self.progressbar)
        widget.layout().addWidget(self.zoomSlider)
        self.statusBar().addWidget(widget, 1)
    

    Screenshot:

    enter image description here