Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QMessageBox is not displaying whole title

#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
like image 456
Ashot Avatar asked Jul 13 '15 09:07

Ashot


3 Answers

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);
            }
        }
    };
like image 150
goGud Avatar answered Sep 30 '22 20:09

goGud


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.

like image 29
TheDarkKnight Avatar answered Sep 30 '22 19:09

TheDarkKnight


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();
}
like image 27
Kuba hasn't forgotten Monica Avatar answered Sep 30 '22 19:09

Kuba hasn't forgotten Monica