PDA

View Full Version : Resizing a QMessageBox?



Caius Aérobus
19th October 2009, 16:16
Hello,
I would like to enlarge a QMessageBox but it seems that setMinimumWidth() does not work in this case. I may of course define a new class and the sizeHint() method but I hope there is a simpler way to achieve it. Any help?

wysota
20th October 2009, 01:12
What exactly did you try? By the way, reimplementing sizeHint() probably won't do anything as it works only for widgets inside layouts. You should be able to use QWidget::resize() though.

wagmare
20th October 2009, 09:06
check if QMessageBox::showEvent() may block the resize event ... qt version ..

Caius Aérobus
20th October 2009, 17:11
The resize() does not work, I checked QMessageBox::showEvent() but nothing special, and QMessageBox::resizeEvent() just calls QDialog::resizeEvent(). So except if dialogs does not allow resize events, I do not understand why I can't resize my QMessageBox.
I use Qt-4.5 on Max OS X Leopard.

wysota
20th October 2009, 17:56
It probably has a constraint set on its layout. One way or another - what is the reason for wanting to resize the dialog? Also maybe it's easier to create your own one from scratch?

Caius Aérobus
20th October 2009, 18:05
Just because my text is too long and QMessageBox resizes its height accordingly and I simply cannot read the end of my text.
Ok I can rewrite anything from scratch but I expect from Qt some easier way to achieve things and being able to resize a window did not appear as a so difficult task lol

wysota
20th October 2009, 18:15
Have you tried simply wrapping your text into a paragraph tag?


#include <QMessageBox>
#include <QApplication>

int main(int argc, char **argv){
QApplication app(argc, argv);
QMessageBox::information(0, "caption", "<p>A very (...) long text</p>");
}

Caius Aérobus
20th October 2009, 18:21
This is exactly what I did first. But since the window was too long, I tried to instanciate a new QMessageBox so as to be able to enlarge it.

wysota
20th October 2009, 18:48
Doesn't QMessageBox break the text for you?

Caius Aérobus
20th October 2009, 19:09
Doesn't QMessageBox break the text for you?

No, the window is simply too high and its bottom goes beyond the bottom of my screen.

wysota
20th October 2009, 19:29
Did it come to your mind that you may simply be trying to fit too much text into the message box? It is meant for short messages, not esseys. Maybe a custom dialog with QPlainTextEdit or QTextBrowser would be better in your case?

ptorrance
10th March 2010, 16:39
A really nasty hack to make QMessageBox resizable by overriding QMessageBox::event().

The following code resets min/max heights and size policys of the derived QMessageBox on EVERY event calls.
It is slow, but it does work...





MyMessageBox::MyMessageBox() : QMessageBox()
{
setSizeGripEnabled(true);
}

bool MyMessageBox::event(QEvent *e)
{
bool result = QMessageBox::event(e);

setMinimumHeight(0);
setMaximumHeight(QWIDGETSIZE_MAX);
setSizePolicy(QSizePolicy::Expanding,QSizePolicy:: Expanding);

// make the detailed text expanding
QTextEdit *textEdit = findChild<QTextEdit *>();

if(textEdit)
{
textEdit->setMinimumHeight(0);
textEdit->setMaximumHeight(QWIDGETSIZE_MAX);
textEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy:: Expanding);
}

return result;
}

Tomasz
26th August 2010, 10:43
I was searching for something like that. How should *.h file look?

thanks in advance
best regards
Tomasz

wysota
29th August 2010, 21:25
I really would avoid using this code. Is seems awfully incorrect to me to force resize of the message box upon any possible event received. To be honest I don't have any problem in fitting large amount of text into a message box without any code manipulations to QMessageBox. Especially that there is QMessageBox::setDetailedText()

franksch
23rd January 2011, 04:51
I was running into the same problem. The width of the QMessageBox appears to be based on the width of the main text. If the InformativeText is significantly longer (as I would expect it to be), it will be wrapped. After reading this thread and other solutions from other sources, I created the following MessageBox class that appear to work:


class MessageBox : public QMessageBox
{
Q_OBJECT
public:
explicit MessageBox(QWidget *parent = 0);
void setMinimumWidth(int minw);

protected:
void showEvent(QShowEvent *event);

private:
int width;
};


MessageBox::MessageBox(QWidget *parent) : QMessageBox(parent)
{
width = 0;
}

void MessageBox::setMinimumWidth(int minw)
{
width = minw;
}

void MessageBox::showEvent(QShowEvent *event)
{
QMessageBox::showEvent(event);
QWidget *textField = findChild<QWidget *>("qt_msgbox_label");
int curWidth = textField->width();
textField->setMinimumWidth(width);
textField->setSizePolicy(QSizePolicy::Expanding,QSizePolicy:: Expanding);
int newWidth = textField->width();
this->move(pos().x() - (newWidth - curWidth) / 2, pos().y());
}

Hope this helps

ArmanS
13th September 2013, 02:26
Long time has passed since the original post, although QMessageBox is still not resizable (when main text is short but the detailed one is very long, it becomes very uncomfortable to use this dialog box).
So based on the few posts above, I'd like to share the "least-evil" way of making QMessageBox resizable.




class QMessageBoxResize: public QMessageBox
{
public:
QMessageBoxResize() {
setMouseTracking(true);
setSizeGripEnabled(true);
}
private:
virtual bool event(QEvent *e) {
bool res = QMessageBox::event(e);
switch (e->type()) {
case QEvent::MouseMove:
case QEvent::MouseButtonPress:
setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
if (QWidget *textEdit = findChild<QTextEdit *>()) {
textEdit->setMaximumHeight(QWIDGETSIZE_MAX);
}
}
return res;
}
};