Search code examples
pythonqtprogress-barpysideqmessagebox

How to subclass QMessageBox and add a progress bar in PySide


I'm rather new to PySide, and Qt in general. I want to add a QProgressBar to a QMessageBox where the buttons would usually be. I'm hoping that there's some way to subclass QMessageBox and change it's layout, but I've never done a Qt layout in code, I've done everything with Qt Designer and pyside-uic.

I've created a concept in Qt Designer, I'd like to have something similar to this done by sub-classing QMessageBox. I've looked at QProgressDialog, but it's too inflexible. I'd like to be able to use the QMessageBox Icon enum for the icon.

Concept


Solution

  • QMessageBox uses a QGridLayout. So, you can add your QProgressBar to its layout:

    msgBox = QMessageBox( QMessageBox.Warning, "My title", "My text.", QMessageBox.NoButton )
    
    # Get the layout
    l = msgBox.layout()
    
    # Hide the default button
    l.itemAtPosition( l.rowCount() - 1, 0 ).widget().hide()
    
    progress = QProgressBar()
    
    # Add the progress bar at the bottom (last row + 1) and first column with column span
    l.addWidget(progress,l.rowCount(), 0, 1, l.columnCount(), Qt.AlignCenter )
    
    msgBox.show()
    

    You can also remove the buttons msgBox.setStandardButtons( QMessageBox.NoButton ). But the close button will be disabled, too...