3

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.

English Buttons French Buttons

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();

This gives me this: French Buttons Resized

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 QPushButtons to not be resized properly.

cechow
  • 170
  • 8

1 Answers1

3

There is no need for using stylesheets, although you will still have to create the object manually instead of using QMessageBox::question.

You can change the message box layout to expand automatically using QLayout::setSizeConstraint. This will force the dialog to resize and fit its content.

Example (it can also be found here):

#include <QtWidgets/QApplication>
#include <qmessagebox.h>
#include <qlayout.h>

int main(int argc, char* argv[])
{
  QApplication a(argc, argv);

  QMessageBox::StandardButtons iButtons = QMessageBox::Save | QMessageBox::Abort | QMessageBox::Cancel;
  QMessageBox msgClose( QMessageBox::Question, "Test", "Test button translation resizing.", iButtons );
  msgClose.setButtonText(QMessageBox::Save, "Save: super mega long text for testing");
  msgClose.setButtonText(QMessageBox::Cancel, "Cancel: another super mega long text for testing");
  msgClose.layout()->setSizeConstraint(QLayout::SetMinimumSize); // QLayout::SetFixedSize also works
  msgClose.exec();

  return 0;
}
cbuchart
  • 10,847
  • 9
  • 53
  • 93
  • I was able to get the translated buttons to be resized properly with your answer when I tested with `Save`, `Abort`, and `Cancel`. I ran into problems when I tried to apply it to my working scenario. It seems to only work properly for the translated text of "Discard" ("Ne pas tenir compte") if there is just one other button (e.g. `Save`+`Discard` or `Discard`+`Cancel`) but not when I have all 3 buttons (`Save`, `Discard`, `Cancel`). I finally got it to work by adding padding with `msgClose.setStyleSheet( "QPushButton {padding: 1px;}" );` but not sure if this is the correct way of doing it? – cechow Aug 07 '18 at 17:03
  • Sorry, after wrestling with it, I have to say that this is not an actual solution to my problem. It seems that the surrounding buttons affect the length and perhaps the message box itself has some fixed size based on the message text itself and once the buttons hit that length, it doesn't extend more. – cechow Aug 09 '18 at 14:42
  • Curious, as you can see in the test, I included super long labels... maybe there is something else like the style/stylesheet used or the windows manager? I’m unable now to make further test for the following two weeks ;) – cbuchart Aug 09 '18 at 15:04
  • You might be right, there is a stylesheet that is being used. I'll see if there's anything in there that's blocking the resize, thanks! – cechow Aug 09 '18 at 16:43
  • 1
    Yup, it was the stylesheet causing the issue, I have it resizing properly now. Thanks for your help. – cechow Aug 09 '18 at 18:07