+ Reply to Thread
Results 1 to 16 of 16

Thread: Resizing a QMessageBox?

  1. #1
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Resizing a QMessageBox?

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,806
    Thanks
    3
    Thanked 4,772 Times in 4,570 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Resizing a QMessageBox?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    633
    Thanks
    108
    Thanked 41 Times in 40 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resizing a QMessageBox?

    check if QMessageBox::showEvent() may block the resize event ... qt version ..
    Last edited by wagmare; 20th October 2009 at 09:11.
    "Behind every great fortune lies a crime" - Balzac

  4. #4
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Resizing a QMessageBox?

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,806
    Thanks
    3
    Thanked 4,772 Times in 4,570 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Resizing a QMessageBox?

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Resizing a QMessageBox?

    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

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,806
    Thanks
    3
    Thanked 4,772 Times in 4,570 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Resizing a QMessageBox?

    Have you tried simply wrapping your text into a paragraph tag?

    Qt Code:
    1. #include <QMessageBox>
    2. #include <QApplication>
    3.  
    4. int main(int argc, char **argv){
    5. QApplication app(argc, argv);
    6. QMessageBox::information(0, "caption", "<p>A very (...) long text</p>");
    7. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Resizing a QMessageBox?

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,806
    Thanks
    3
    Thanked 4,772 Times in 4,570 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Resizing a QMessageBox?

    Doesn't QMessageBox break the text for you?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Resizing a QMessageBox?

    Quote Originally Posted by wysota View Post
    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.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,806
    Thanks
    3
    Thanked 4,772 Times in 4,570 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Resizing a QMessageBox?

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    May 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Resizing a QMessageBox?

    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...


    Qt Code:
    1. MyMessageBox::MyMessageBox() : QMessageBox()
    2. {
    3. setSizeGripEnabled(true);
    4. }
    5.  
    6. bool MyMessageBox::event(QEvent *e)
    7. {
    8. bool result = QMessageBox::event(e);
    9.  
    10. setMinimumHeight(0);
    11. setMaximumHeight(QWIDGETSIZE_MAX);
    12. setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    13.  
    14. // make the detailed text expanding
    15. QTextEdit *textEdit = findChild<QTextEdit *>();
    16.  
    17. if(textEdit)
    18. {
    19. textEdit->setMinimumHeight(0);
    20. textEdit->setMaximumHeight(QWIDGETSIZE_MAX);
    21. textEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    22. }
    23.  
    24. return result;
    25. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ptorrance; 10th March 2010 at 16:52.

  13. #13
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Resizing a QMessageBox?

    I was searching for something like that. How should *.h file look?

    thanks in advance
    best regards
    Tomasz

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    31,806
    Thanks
    3
    Thanked 4,772 Times in 4,570 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Resizing a QMessageBox?

    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()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. #15
    Join Date
    Jan 2011
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Angry Re: Resizing a QMessageBox?

    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:

    Qt Code:
    1. class MessageBox : public QMessageBox
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MessageBox(QWidget *parent = 0);
    6. void setMinimumWidth(int minw);
    7.  
    8. protected:
    9. void showEvent(QShowEvent *event);
    10.  
    11. private:
    12. int width;
    13. };
    14.  
    15.  
    16. MessageBox::MessageBox(QWidget *parent) : QMessageBox(parent)
    17. {
    18. width = 0;
    19. }
    20.  
    21. void MessageBox::setMinimumWidth(int minw)
    22. {
    23. width = minw;
    24. }
    25.  
    26. void MessageBox::showEvent(QShowEvent *event)
    27. {
    28. QMessageBox::showEvent(event);
    29. QWidget *textField = findChild<QWidget *>("qt_msgbox_label");
    30. int curWidth = textField->width();
    31. textField->setMinimumWidth(width);
    32. textField->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    33. int newWidth = textField->width();
    34. this->move(pos().x() - (newWidth - curWidth) / 2, pos().y());
    35. }
    To copy to clipboard, switch view to plain text mode 
    Hope this helps

  16. The following user says thank you to franksch for this useful post:

    Chromatix (3rd September 2014)

  17. #16
    Join Date
    Mar 2009
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resizing a QMessageBox?

    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.


    Qt Code:
    1. class QMessageBoxResize: public QMessageBox
    2. {
    3. public:
    4. QMessageBoxResize() {
    5. setMouseTracking(true);
    6. setSizeGripEnabled(true);
    7. }
    8. private:
    9. virtual bool event(QEvent *e) {
    10. bool res = QMessageBox::event(e);
    11. switch (e->type()) {
    12. case QEvent::MouseMove:
    13. case QEvent::MouseButtonPress:
    14. setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
    15. if (QWidget *textEdit = findChild<QTextEdit *>()) {
    16. textEdit->setMaximumHeight(QWIDGETSIZE_MAX);
    17. }
    18. }
    19. return res;
    20. }
    21. };
    To copy to clipboard, switch view to plain text mode 

  18. The following user says thank you to ArmanS for this useful post:

    Chromatix (3rd September 2014)

+ Reply to Thread

Similar Threads

  1. QMessageBox misbehaves after QFileDialog call
    By MikeG in forum Qt Programming
    Replies: 8
    Last Post: 2nd June 2009, 11:17
  2. Re: Help on QMessageBox
    By arunvv in forum Newbie
    Replies: 2
    Last Post: 26th March 2008, 00:45
  3. QMessageBox problem in Qtopia
    By jogeshwarakundi in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 8th February 2008, 10:22
  4. Replies: 2
    Last Post: 14th August 2007, 16:16
  5. QMdiSubWindow resizing in 4.3
    By kalpa in forum Qt Programming
    Replies: 0
    Last Post: 4th June 2007, 14:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts