PDA

View Full Version : QMessageBox: Controlling the width



ChrisW67
8th July 2009, 00:25
Hi All,

This is a complete noob question and I'm no doubt going to smack my forehead when I hear the answer.

I am creating a QMessageBox in various places to present errors and warnings. The boxes seem to have a mind of their own when it comes to sizing (width) and often wrap poorly or truncate the window title. I have tried using setMinimumWidth() but it does not seem to have any effect. Code looks like:
#include <QtGui>
#include <QDebug>

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

QApplication app(argc, argv);

QMessageBox msgBox1;
msgBox1.setMinimumWidth(400); // seemingly ignored, always same width box
msgBox1.setWindowTitle("Width 400");
msgBox1.setText("<strong>Warning</strong>");
msgBox1.setInformativeText(
"You have not entered hours against this event<br /> "
"Do you wish to save the event anyway?");
msgBox1.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox1.setDefaultButton(QMessageBox::No);
msgBox1.setIcon(QMessageBox::Warning);
msgBox1.show();

QMessageBox msgBox2;
msgBox2.setMinimumWidth(800); // seemingly ignored, always same width box
msgBox2.setWindowTitle("Width 800");
msgBox2.setText("<strong>Warning</strong>");
msgBox2.setInformativeText(
"You have not entered hours against this event<br /> "
"Do you wish to save the event anyway?");
msgBox2.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox2.setDefaultButton(QMessageBox::No);
msgBox2.setIcon(QMessageBox::Warning);
msgBox2.exec();

return app.exec();
}

// vi: sw=4 ts=4 et


The result is attached. What is the obvious thing I'm missing?

Chris

nish
8th July 2009, 02:16
have you tried setFixedSize()?

ChrisW67
8th July 2009, 08:16
I have now, same result :(

This is on Linux and X11. Possibly related to the quirks restoring top-level window position and size described in the docs under "Window Geometry"

nish
8th July 2009, 08:24
as a quick hack.. grab the layout of the msgbox by msgbox->layout()... and add a dummy widget to that layout

SunnySan
8th July 2009, 12:24
just quickly tried your code on XP .
same thing happened

changed also to
msgBox2.setMaximumWidth(1800);

no change

nish
8th July 2009, 12:43
for that hack to work first you need to set the dummy widget size to something..


QWidget *dw=new QWidget
dw->setFixedSize(100,100);
QLayout* l = msgBox->layout();
l->addWidget(dw);

or better use combobox or listview instead of widget... i just want to know what happens..

ChrisW67
9th July 2009, 00:24
#include <QtGui>
#include <QDebug>

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

QApplication app(argc, argv);

QMessageBox msgBox1;
// msgBox1.setMinimumWidth(400); // seemingly ignored, always same width box
// msgBox1.setFixedSize(QSize(400, 200)); // also no effect
// msgBox1.setFixedSize(400, 200);

QPushButton dw;
dw.setFixedSize(100, 100);
msgBox1.layout()->addWidget(&dw);

msgBox1.setWindowTitle("Width 400");
msgBox1.setText("<strong>Warning</strong>");
msgBox1.setInformativeText(
"You have not entered hours against this event<br /> "
"Do you wish to save the event anyway?");
msgBox1.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox1.setDefaultButton(QMessageBox::No);
msgBox1.setIcon(QMessageBox::Warning);
msgBox1.show();

QMessageBox msgBox2;
// msgBox2.setMinimumWidth(800); // seemingly ignored, always same width box
// msgBox2.setFixedSize(QSize(800, 200));
// msgBox1.setFixedSize(800, 200);
msgBox2.setWindowTitle("Width 800");
msgBox2.setText("<strong>Warning</strong>");
msgBox2.setInformativeText(
"You have not entered hours against this event<br /> "
"Do you wish to save the event anyway?");
msgBox2.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox2.setDefaultButton(QMessageBox::No);
msgBox2.setIcon(QMessageBox::Warning);
msgBox2.exec();

return app.exec();
}

// vi: sw=4 ts=4 et


and result is attached. Not sure it proves anything though.

nish
9th July 2009, 01:46
this proves that the messagebox is not calling setFixedSize() internally. So i think we cannot do anything..

the only way to debug now it to study qmessagebox.cpp and qmessagebox.h in, may be add these files to your project and debug, so that you can safely make some modifications.

thePoet
14th August 2009, 22:38
Turns out the layout box in the QMessageBox is a grid (or at least it is in 4.5.2). You can force the resize using a spacer this way:



QMessageBox msgBox;
QSpacerItem* horizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
msgBox.setText( "SomText" );
QGridLayout* layout = (QGridLayout*)msgBox.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
msgBox.exec();


That will force the width to a minumum of 500, but it is allowed to be larger.