setting the size of a QMessageBox
-
Hi,
I am new to QT, but I am an old Apple Objective C and Cocoa programmer; I am 57 years old. I have done my diligence to find a way to change the rect of a QMessageBox, but have no solution. What is with the QMessageBox size? I have 2 standard buttons, Yes, and No. The Information is smashed into a small rectangle... and for some reason I want the rectangle bigger.
setBaseSize(QSize); does nothing. It has no setSize(QSize) member function... right?
Craig
-
QMessageBox does not provide much customization.
Its goal is to show messages in the most compact way allowed by style.
The most direct way to have custom appearance is to subclass QDialog.Saying this you might try to use stylesheets to customize it.
http://doc.qt.io/qt-4.8/stylesheet-examples.htmlFor example:
min-width: 10em;
may allow you to increase button size. -
Well you can fiddle with its layout object and force it to become bigger.
Only tested on windows.QMessageBox msgBox; msgBox.setStandardButtons( QMessageBox::Yes|QMessageBox::No ); 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();
-
Hi and welcome to devnet,
Can you share the setup of your QMessageBox ?
-
Here it is. I am teaching my son computer programming. It is a simple program. I know I am being fussy, but what the heck.
QMessageBox msgBox; msgBox.setText("The deck is empty!"); msgBox.setInformativeText("Do you want to start a new deck?"); msgBox.setIcon(QMessageBox::Question); msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); msgBox.setBaseSize(QSize(600, 120)); int r = msgBox.exec(); if(r==QMessageBox::Yes) { dealer->newGame(); updateCardCount(); clearDealerAndPlayerViews(); } else { ui->actionHit->setDisabled(true); ui->actionStay->setDisabled(true); }
-
@CraigBakalian just to be sure, the wrong size did happen on linux or OS X ? (because you mentioned Objective-C I assumed you were running OS X)
-
@CraigBakalian
Well it insert a spacer with a minimum size to force the dialog bigger.
Should not mess with the buttons.
However
What could make it fail if that they change the layout object from
QGridLayout to something else later on.
So I consider it a hax as it uses internal details of the QMessageBox. -
@SGaist
No. I have dropped the whole OS X thing. I am a big linux fan. Much more open. The wrong size happened on linux. But, to say it is the wrong size would be wrong. It just reduced down to a small rect with the InformativeText in two lines. It looks squashed. Is there a way to resize the QMessageBox without any hacks? -
As I already said QMessageBox resizes itself to fit minimum possible rectangle
using internal components dimensions and size policies,So minimum size (etc) set directly to QMessageBox will be ignored.
And you basically are asking to hack this widget behaviour.The only choices you have not counting writing your own QMessageBox alternative is either use hack or define size policies/minimum size to internal controls instead.
That thing can be done with or without stylesheet.
change i value below to see some options.QMessageBox msgBox; int i =1; switch( i ) { case 1: msgBox.setStyleSheet(
"QPushButton {"
" background-color: red;"
" border-style: outset;"
" border-width: 2px;"
" border-radius: 10px;"
" border-color: beige;"
" font: bold 14px;"
" min-width: 10em;"
" padding: 6px;"
"}"
);
break;
case 2:msgBox.setStyleSheet(
" QLabel {"
" min-width: 300em;"
"}"
);break; case 3: { QGridLayout* layout = (QGridLayout*)msgBox.layout(); layout->setColumnMinimumWidth( 1, 800); } break; default: ;
}
msgBox.setText("The deck is empty!"); msgBox.setInformativeText("Do you want to start a new deck?"); msgBox.setIcon(QMessageBox::Question); msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); int r = msgBox.exec();
-
One method that worked for me was to subclass QMessageBox and control the size with modified show and resize events:
TMessageBox::TMessageBox( const QWidget *Parent) :QMessageBox(const_cast<QWidget*>(Parent)) { d_FixedWidth = 500; this->setTextFormat(Qt::RichText); this->setStyleSheet("dialogbuttonbox-buttons-have-icons: 0;\nbutton-layout: 1;"); } void TMessageBox::showEvent( QShowEvent *Event) { QMessageBox::showEvent(Event); this->setFixedWidth(d_FixedWidth); } void TMessageBox::resizeEvent( QResizeEvent *Event) { QMessageBox::resizeEvent(Event); this->setFixedWidth(d_FixedWidth); }
I didn't find another method that worked. I did find that the size must be set each time a resize or show event was received otherwise it would default back to whatever it decided to use.
I removed the fixed size completely about a year ago (I don't remember exactly why but it was not because it didn't work - there was some other reason). It is nice to have a constant look to the message boxes and not have them shrunk down to a minimum size.
Some other changes unrelated to your post ...
The style sheet command in the constructor turns off the button icons and sets the format to OSX layout regardless of the platform it is run on ('button-layout: 1;'). Probably not a good idea to force the button layout to something non-native but I always perferred the OSX version (with detailed text on left, options on right). I hate icons on buttons (on any software) so this was something I wanted to kill.
I use rich text for the members 'setText' and 'setInformativeText' so that it stands out a bit better. These member functions are subclassed to wrap the supplied text with RTF tags before passing to QMessageBox. Member 'setText()' is set to a larger bold font meant to have a very short description such as 'Error Loading File'. Member 'setInformativeText()' has a bit more detail regarding whatever I am trying to comunicate such as 'File 'name.txt' has an invalid entry'. The detailed text section of the messagebox, if used, contains all the details from the message and can contain quite a bit of text.
-
@Rondog said:
setFixedWidth
If you want to subclass, I would check if it is sufficient
to override
void MyMessagbeBox::setFixedSize ( int w, int h )Within it you can check if minimum size was set on the widget,
then if it was compare it to w and h and increase them if necessary
then call original QMessageBpx version.This should do the job.