I am currently working on localization of an application. Everything's translating as I expected, however, the QMessageBox
doesn't resize the buttons to fit the text.
This is the code I'm using to generate the question box, the QTranslator
is wrapped up where MM_TR
is defined:
#include <QMessageBox>
void MainWindow::closeEvent( QCloseEvent * pEvent ) {
QMessageBox::StandardButtons iButtons = QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel;
QMessageBox::StandardButton iDefaultButton = QMessageBox::Save;
QMessageBox::StandardButton iButton = QMessageBox::question( this, QString::fromStdString( MM_TR( "ctxMessageBoxQuestion", "Save changes?" ) ), QString::fromStdString( MM_TR( "ctxMessageBoxQuestion", "Project has been modified, save changes?" ) ), iButtons, iDefaultButton );
}
I've searched through the internet for anyone experiencing the same problem but haven't found anything conclusive so far. I've tried setting the size policy to both Minimum
and MinimumExpanding
but that doesn't work either. The only thing that sort of worked was setting the stylesheet, which I tried with the following code:
QMessageBox::StandardButtons iButtons = QMessageBox::Save | QMessageBox::Abort | QMessageBox::Cancel;
QMessageBox msgClose( QMessageBox::Question, "Test", "Test button translation resizing.", iButtons );
msgClose.setStyleSheet( "QPushButton {min-width:100;}" );
I don't think the correct way of doing things is to manually set the minimum width based on whatever language comes up, though, so I'd prefer not to do that. This also changes it for all push buttons, which is not exactly what I want.
I'm wondering at this point if the only option is for me to create a custom dialog box?
UPDATE: My final solution includes cbuchart's answer as well as a stylesheet padding setting:
QMessageBox::StandardButtons iButtons = QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel;
QMessageBox msgClose( QMessageBox::Question, QString::fromStdString( MM_TR( "ctxMessageBoxQuestion", "Save changes?" ) ), QString::fromStdString( MM_TR( "ctxMessageBoxQuestion", "Project has been modified, save changes?" ) ), iButtons );
msgClose.setStyleSheet( "QPushButton {padding: 3px;}" );
msgClose.layout()->setSizeConstraint( QLayout::SizeConstraint::SetMinimumSize );
QMessageBox::StandardButton iButton = (QMessageBox::StandardButton)msgClose.exec();
The thing to note is that if padding is increased too much, it will start covering the text - which I don't really understand - but 3px seems to be good.
UPDATE 2:
After playing around with it, I think QMessageBox
has some fixed width that's linked to the message box text itself that cannot be modified. The buttons resize and fits the button text if the message box text is long enough so it seems like the button resize has nothing to do with what the button text itself.
I tried adjusting with setMinimumWidth
and setFixedWidth
and the box just doesn't resize. Based on comments in this bug QTBUG-7851, I think QMessageBox
can't be resized programmatically. It would be great if anyone knows an actual solution to this that doesn't include making a custom dialog.
UPDATE 3:
Based on cbuchart's comments, I realized that there was a .qss stylesheet that had a min-width
setting causing the QPushButton
s to not be resized properly.