Qt World Summit: Submit your Presentation

How to resize QMessageBox

  • Hello,
    My issue is, size of the QMessageBox. I working Windows 7 no problem, I tried the same software with Ubuntu. window size of QMessageBox very small.
    How can I adjust the size of the window?

       if (ud.checkUser(u)==false){
            QMessageBox::critical(this, "DİKKAT", "Giriş Başarısız");

    0_1475065267614_qmsgbox.PNG 0_1475065306434_qmsgbox.PNG 0_1475065363156_qmsgbox2.PNG

  • Images upload in this forum does not work I'm afraid you have to use an external uploader if you want us to see images.

    The first thing that tingles my ear is non-ascii characters in a literal.

    Could you try and replace "DİKKAT" with "Warning" and "Giriş Başarısız" with "Login Failed" and see if the size is correct?

  • I tried and replaced but no change size of QmessageBox

  • Lifetime Qt Champion

    for some reason i could see them

  • Hi,
    you can pad the text in the body of the message box with blanks on the left and right to not get "Dİ..." in the title.

  • Hi Michael,
    I put blank on the left and right but no change in this situation.

  • Moderators


    QMessageBox messageBox(icon, title, text, QMessageBox::Ok, parent);
    messageBox.setMinimumSize(200, 0);
    if (messageBox.exec() == QMessageBox::Ok)  {
        // Ok clicked

  • I wanted to have a fixed width QMessageBox back in the 4.x days of Qt but I couldn't do this without subclassing QMessageBox and updating the width in the resizeEvent():

    void TMessageBox::resizeEvent(QResizeEvent *Event)
    // where 'd_FixedWidth' is the width in pixels.

    I still use this and it works fine (currently Qt 5.6.0) but I probably should revisit this 'hack'. My goal was to have better control over the message box width and to have something that had a (somewhat) unique visual style.

  • @Rondog I can't tell you how much I hate it when applications do that. As soon as someone uses a different font for the desktop theme than you the UI is broken.

  • @Wieland I agree this is bad in general but, sometimes, having an application specific theme is a desirable goal and can be positive.

    For example I was working with one software where everything (dialog's, windows, background images, and even the window controls) where unique to the application. It can give it a certain feel and appeal which didn't seem too bad. It can be horrible if taken too far but if done properly it can be quite decent. Even Qt supports this idea somewhat (you can use the 'Fusion' theme in your Qt application if you are so inclined across all OS's).

    Using a modified QMessageBox should not be a big deal provided it works. I would never have done this if the Windows version of the QMessageBox was not so annoyingly ugly.

  • Moderators

    He was referring to the fixed size of the dialog. It may very well happen (and it does) that the contents don't fit in the fixed width if the font is changed from the default.

  • @kshegunov The width of the QMessageBox is fixed (not the height). If the text doesn't fit it wraps to the next line and the QMessageBox grows in height. If the font is set so big that it cannot be displayed then there are other issues - lol.

    I agree stuff like this should be avoided but in this case it is a low risk. I didn't start this thread so I assume others don't like the ugly QMessageBox dialogs on Windows either.

  • @alper39 said in How to resize QMessageBox:

    Hi Michael,
    I put blank on the left and right but no change in this situation.

    On Windows this works using simple blanks:

    QMessageBox::critical(nullptr, qa("title"), qa("              text             "));

    If it does not work on other systems I would consider it to be a bug.

  • @m-sue
    Thank you Michael,
    You said true.

    QMessageBox::critical(this, "Warning", " Login Failed ");

    Firstly, I put blank on the left and right "Warning" but no change in this situation.
    Later, I put blank on the left and right " Login Failed "
    It is ok

Log in to reply