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

流过昼夜 提交于 2020-01-05 05:19:21

问题


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:



来源:https://stackoverflow.com/questions/45791136/pyqt5-how-to-align-the-elements-of-the-status-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!