7
#include <QtGui>

int main(int argc, char** argv) 
{
    QApplication app(argc, argv);

    // first
    QMessageBox box(0);
    box.setText("short text");
    box.setWindowTitle("looooooooooooooooong text");
    box.setMinimumSize(800, 0);

    box.exec();


    // second
    //QMessageBox::warning(0, "looooooooooooooooong text", "short text");

    return app.exec();
}

enter image description here

Both approaches produce this messagebox, where title is not displayed properly. I have tried to resize dialog widget by it doesn't help. How can I force QMessageBox to display whole title?

As a workaround I can add trailing spaces to title text but I think there should be better solution.

aminasya@aminasya-desktop:~/qt$ qmake --version
QMake version 2.01a
Using Qt version 4.8.6 in /usr/lib/x86_64-linux-gnu
demonplus
  • 5,613
  • 12
  • 49
  • 68
Ashot
  • 10,807
  • 14
  • 66
  • 117

4 Answers4

1

I tried to create QMessageBox with constructor which qt help mentioned in qmessagebox.cpp but it didnt worked for me too. For some reason QMessageBox adjust size to fit the window title doesn't work. However you can adjust MessageBox size by creating your own sublclass of QMessageBox.

Please see example below;

class MyMessageBox : public QMessageBox
    {
    public:
        explicit MyMessageBox(QWidget *parent = 0) : QMessageBox(parent) { }
        MyMessageBox(const QString &title, const QString &text, Icon icon,
                     int button0, int button1, int button2,
                     QWidget *parent = 0,
                     Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint) :
            QMessageBox(title, text, icon, button0, button1, button2, parent, f) { }


        static void about(QString title, QString text)
        {
            MyMessageBox aboutBox(title, text, QMessageBox::Information, 0, 0, 0, NULL);

            aboutBox.setText(title);
            aboutBox.setText(text);
            QIcon icon = aboutBox.windowIcon();
            QSize size = icon.actualSize(QSize(64, 64));
            aboutBox.setIconPixmap(icon.pixmap(size));

            aboutBox.exec();
        }

        void showEvent(QShowEvent *event)
        {
            QMessageBox::showEvent(event);
            QWidget *textField = findChild<QWidget *>("qt_msgbox_label");
            if (textField != NULL)
            {
                // getting what ever my system has set for the window title font
                QFont font = QFont("Ubuntu Bold", 11);
                // you might want to make it more generic by detecting the actuall font
                // or using smth like this:
                //QFont font = QApplication::font("QWorkspaceTitleBar");

                QFontMetrics fm(font);
                int width = qMax(fm.width(windowTitle()) + 50, textField->minimumWidth());
                textField->setMinimumWidth(width);
            }
        }
    };
goGud
  • 4,163
  • 11
  • 39
  • 63
  • I got the idea, but can't understand why are you explicitly setting text, title and icon in about method. Arn't they already set in constructor? – Ashot Jul 13 '15 at 14:40
1

It appears that QMessageBox, when launched with exec() checks the length of the body text and automatically adjusts the size, ignoring the fact that the title text may be longer. Though not ideal, you can change it just afterwards with a QTimer, as demonstrated here:

QMessageBox* box = new QMessageBox;
box->setText("short text");
box->setWindowTitle("looooooooooooooooong text");

QTimer* pTimer = new QTimer;
pTimer->setSingleShot(true);
QObject::connect(pTimer, &QTimer::timeout, [=](){
   box->setMinimumWidth(400);
   pTimer->deleteLater();
});
pTimer->start(0);
box->exec();

Since this occurs after the message box is launched, the change in size is visible.

The better solution would simply be to create your own MessageBox, derived from QDialog. After all, the QMessageBox class is just a convenience widget.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • I tried this solution. It worked if I change pTimer->start(30) instead of pTimer->start(0). But it also prints this message `QCoreApplication::postEvent: Unexpected null receiver ` I have modified this code. I have made callback function a member slot function of empty class. – Ashot Jul 13 '15 at 11:24
  • I've just checked and I don't the the warning that you mention, so either it's specific to Linux, or something else is causing it. I suspect the timer being 0 or 30 is a timing issue. Using '0' means that the event is posted as soon as the event queue is processed. If it occurs before the MessageBox is launched, then you wouldn't see any difference. Either way, it sounds like it's working for you ;O) – TheDarkKnight Jul 13 '15 at 12:28
1

Since exec() and show() both override your minimum size based on the contents of the box's text, the simple solution is not to use exec() and to set the minimum size after the box has been shown. In any case, you should never use exec() in dialogs anyway.

Note: Window titles are non-portable. Your UI must still make sense without a window title. For example, on OS X the message box window titles are invisible.

#include <QApplication>
#include <QMessageBox>
#include <QDebug>

int main(int argc, char** argv)
{
  QApplication app(argc, argv);

  QMessageBox box;
  box.setText("short text");
  box.setWindowTitle("looooooooooooooooong text");
  box.show();
  box.setMinimumSize(qMax(box.minimumWidth(), 800), box.minimumHeight());

  return app.exec();
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

Since the message box width is adjusted to the width of the text, a simple answer seems to be to pad the text string with spaces:

QString title = "This is a looooooooooooooooooooooooong title";
QString txt   = "Short Text";
QMessageBox box;
box.setWindowTitle(title);
int titleLen = title.length();
int txtLen   = txt.length();
if (txtLen < titleLen)
{
    int diff = titleLen - txtLen;
    txt.resize(titleLen + (diff * 2),' '); // diff * 2 is extra padding;
}
box.setText(txt);
box.exec();

You can see I kludged the padding length because of variable width fonts. But hey, the kludge works for me.

David Casper
  • 394
  • 2
  • 9