0

I would like to create a QMessageBox::critical which would automatically adapt its size depending on the size of the content (the message text). How can I do it?

This is the message I would like to display on the QMessageBox

std::wstring tmpStrEx;
QDomNode tmpNodeParent = node.parentNode();

while(!tmpNodeParent.isNull() && tmpNodeParent.nodeName().toStdWString() != L"root"){
std::wstring tmpStrTmp;

tmpStrTmp = L"<"
           + tmpNodeParent.nodeName().toStdWString()
           + L" nm=\""
           + tmpNodeParent.toElement().attribute(QS(_T("nm")), QS(_T("undef")) ).toStdWString()
           + L"> ";
}

QString mex = "Error while reading vector.<br/><br/>Please check in:<br/><br/> " + tmpStrTmp ;

tmpStrTmp can be something like <vct nm="name"> <vcx nm="xyz">

I tried in these ways to build the QMessageBox:

First a simple:

QMessageBox::critical(this, "Error", mex);

This shows half message without the content in tmpStrTmp like this:

Error while reading vector.

Please check in:

I thought tmpStrTmp was the problem but if I put only tmpStrTmp in the QMessageBox, it shows its content. So, I thought it was a problem of space and I tried these two ways:

1.

 QMessageBox msgBox;
 QSpacerItem* spacer = new QSpacerItem(500, 500, QSizePolicy::Minimum, QSizePolicy::Expanding);
 msgBox.setTextFormat(Qt::RichText);
 msgBox.setText( mex );
 QGridLayout* layout = (QGridLayout*)msgBox.layout();
 layout->addItem(spacer, layout->rowCount(), 0, 1, layout->columnCount());
 msgBox.exec();

2.

 QMessageBox *box = new QMessageBox::QMessageBox(QMessageBox::critical, "Error", mex);

 QGridLayout *layout = qobject_cast<QGridLayout *>(box->layout());
 if (layout) {
    QTextEdit *edit = new QTextEdit(mex);
    edit->setReadOnly(true);
    layout->addWidget(edit, 0, 1);
 }
 box->exec();
 delete box;

but without success...any help? thanks

user3473325
  • 103
  • 1
  • 8
  • 1
    Did you try something ? – Thomas Ayoub Apr 11 '14 at 15:48
  • 1
    The text is broken to multiple lines according to its length. So you don't want the text be in multiple lines and the messagebox grow horizontally? – Nejat Apr 11 '14 at 15:51
  • 1
    This question appears to be off-topic because there is no attempt shown. It could also be closed for unclear reason. – László Papp Apr 11 '14 at 20:11
  • There may be some related hacks here: http://www.qtcentre.org/threads/22298-QMessageBox-Controlling-the-width but you need to be a little more specific. Message boxes size themselves automatically. Are you trying to word wrap? Does your text have line breaks? Please include the content you are trying to display as well as a screen shot of what you are seeing and a description of what you *want* to see. – Jason C Apr 11 '14 at 20:15

2 Answers2

3

If you don't want your QMessageBox to simply widen out with your text, and break a new line at some point, you should create an instance of QMessageBox and set it to have a fixed width (QWidget::setFixedWidth()).

Another option is to pass in a text string with new lines (\n) in it, and it can handle it nicely.

Also, if you are using an instance, you can tell it that you are using RichText, and pass in a subset of html markups QMessageBox::setTextFormat(Qt::RichText), or even just make sure you use a valid html tag before your first newline character.

Hope that helps!

László Papp
  • 51,870
  • 39
  • 111
  • 135
phyatt
  • 18,472
  • 5
  • 61
  • 80
  • 2
    QMessageBox::critical breaks message text in to multiple lines according to its length by default. – Nejat Apr 11 '14 at 16:31
  • Yes, but sometimes you end up with long skinny text boxes that aren't very appealing. I ended up making them look closer to a 4:3 ratio if I am worried about the way they look. – phyatt Apr 11 '14 at 16:39
1

QMessageBox is a grid. I'm not sure how to adapt QMessageBox size to its text size but if you know what is going to be appear in the messagebox you can most definitely set it to a minimum(actually fixed) size by this way:-

QMessageBox messageBox;
QSpacerItem* horizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
messageBox.setText( "Put text here" );
QGridLayout* layout = (QGridLayout*)messageBox.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
messageBox.exec();
pacmanfordinner
  • 328
  • 1
  • 4
  • 9