Results 1 to 9 of 9

Thread: QMessageBox: Controlling the width

  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Question QMessageBox: Controlling the width

    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:
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6.  
    7. QApplication app(argc, argv);
    8.  
    9. QMessageBox msgBox1;
    10. msgBox1.setMinimumWidth(400); // seemingly ignored, always same width box
    11. msgBox1.setWindowTitle("Width 400");
    12. msgBox1.setText("<strong>Warning</strong>");
    13. msgBox1.setInformativeText(
    14. "You have not entered hours against this event<br /> "
    15. "Do you wish to save the event anyway?");
    16. msgBox1.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    17. msgBox1.setDefaultButton(QMessageBox::No);
    18. msgBox1.setIcon(QMessageBox::Warning);
    19. msgBox1.show();
    20.  
    21. QMessageBox msgBox2;
    22. msgBox2.setMinimumWidth(800); // seemingly ignored, always same width box
    23. msgBox2.setWindowTitle("Width 800");
    24. msgBox2.setText("<strong>Warning</strong>");
    25. msgBox2.setInformativeText(
    26. "You have not entered hours against this event<br /> "
    27. "Do you wish to save the event anyway?");
    28. msgBox2.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    29. msgBox2.setDefaultButton(QMessageBox::No);
    30. msgBox2.setIcon(QMessageBox::Warning);
    31. msgBox2.exec();
    32.  
    33. return app.exec();
    34. }
    35.  
    36. // vi: sw=4 ts=4 et
    To copy to clipboard, switch view to plain text mode 

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

    Chris
    Attached Images Attached Images

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox: Controlling the width

    have you tried setFixedSize()?

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMessageBox: Controlling the width

    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"

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox: Controlling the width

    as a quick hack.. grab the layout of the msgbox by msgbox->layout()... and add a dummy widget to that layout

  5. #5
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMessageBox: Controlling the width

    just quickly tried your code on XP .
    same thing happened

    changed also to
    msgBox2.setMaximumWidth(1800);

    no change
    Last edited by SunnySan; 8th July 2009 at 12:29.

  6. #6
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox: Controlling the width

    for that hack to work first you need to set the dummy widget size to something..

    Qt Code:
    1. dw->setFixedSize(100,100);
    2. QLayout* l = msgBox->layout();
    3. l->addWidget(dw);
    To copy to clipboard, switch view to plain text mode 

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

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMessageBox: Controlling the width

    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6.  
    7. QApplication app(argc, argv);
    8.  
    9. QMessageBox msgBox1;
    10. // msgBox1.setMinimumWidth(400); // seemingly ignored, always same width box
    11. // msgBox1.setFixedSize(QSize(400, 200)); // also no effect
    12. // msgBox1.setFixedSize(400, 200);
    13.  
    14. dw.setFixedSize(100, 100);
    15. msgBox1.layout()->addWidget(&dw);
    16.  
    17. msgBox1.setWindowTitle("Width 400");
    18. msgBox1.setText("<strong>Warning</strong>");
    19. msgBox1.setInformativeText(
    20. "You have not entered hours against this event<br /> "
    21. "Do you wish to save the event anyway?");
    22. msgBox1.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    23. msgBox1.setDefaultButton(QMessageBox::No);
    24. msgBox1.setIcon(QMessageBox::Warning);
    25. msgBox1.show();
    26.  
    27. QMessageBox msgBox2;
    28. // msgBox2.setMinimumWidth(800); // seemingly ignored, always same width box
    29. // msgBox2.setFixedSize(QSize(800, 200));
    30. // msgBox1.setFixedSize(800, 200);
    31. msgBox2.setWindowTitle("Width 800");
    32. msgBox2.setText("<strong>Warning</strong>");
    33. msgBox2.setInformativeText(
    34. "You have not entered hours against this event<br /> "
    35. "Do you wish to save the event anyway?");
    36. msgBox2.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    37. msgBox2.setDefaultButton(QMessageBox::No);
    38. msgBox2.setIcon(QMessageBox::Warning);
    39. msgBox2.exec();
    40.  
    41. return app.exec();
    42. }
    43.  
    44. // vi: sw=4 ts=4 et
    To copy to clipboard, switch view to plain text mode 

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

  8. #8
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMessageBox: Controlling the width

    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.

  9. #9
    Join Date
    Jul 2009
    Posts
    10
    Thanked 1 Time in 1 Post

    Default Re: QMessageBox: Controlling the width

    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:

    Qt Code:
    1. QMessageBox msgBox;
    2. QSpacerItem* horizontalSpacer = new QSpacerItem(500, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
    3. msgBox.setText( "SomText" );
    4. QGridLayout* layout = (QGridLayout*)msgBox.layout();
    5. layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
    6. msgBox.exec();
    To copy to clipboard, switch view to plain text mode 

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

  10. The following user says thank you to thePoet for this useful post:

    astodolski (12th June 2014)

Similar Threads

  1. QMessageBox misbehaves after QFileDialog call
    By MikeG in forum Qt Programming
    Replies: 8
    Last Post: 2nd June 2009, 10:17
  2. Replies: 4
    Last Post: 15th October 2008, 13:24
  3. Scaling of pen width in QGraphicsView
    By spuds in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2008, 01:47
  4. QTableWidget column width and resizing
    By shooogun in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2008, 22:31
  5. How to obtain the width of a QTableWidget?
    By Giel Peters in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2006, 22:34

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.