Pergunta

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

Foi útil?

Solução

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...

Outras dicas

you can always add your own layout to the QProgressDialog.

layout = QtGui.QHBoxLayout()
self.setLayout(layout)

# layout.setContentsMargins(0, 0, 0, 0)
layout.setAlignment(QtCore.Qt.AlignTop)

icon = QtGui.QApplication.style().standardIcon(QtGui.QStyle.SP_FileIcon)
self.label = QtGui.QLabel()
self.label.setPixmap(icon.pixmap(16, 16))
self.label2 = QtGui.QLabel("My Label")
layout.addWidget(self.label)
layout.addWidget(self.label2)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top