PyQt5: How to align the elements of the status bar

2019-07-24 16:55发布

问题:

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:

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

回答1:

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: