Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. setting the size of a QMessageBox
Forum Updated to NodeBB v4.3 + New Features

setting the size of a QMessageBox

Scheduled Pinned Locked Moved General and Desktop
13 Posts 5 Posters 39.7k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    Hi and welcome to devnet,

    Can you share the setup of your QMessageBox ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • C Offline
      C Offline
      CraigBakalian
      wrote on last edited by
      #5

      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);
      }
      
      1 Reply Last reply
      0
      • mrjjM mrjj

        @CraigBakalian

        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();
        
        
        C Offline
        C Offline
        CraigBakalian
        wrote on last edited by
        #6

        @mrjj

        It is 6 PM here. I will try the snip of code tomorrow. And I am running linux, ubuntu. It looks like it should work. I hope it doesn't mess with the general layout of the button objects.

        mrjjM 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #7

          @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)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          0
          • C CraigBakalian

            @mrjj

            It is 6 PM here. I will try the snip of code tomorrow. And I am running linux, ubuntu. It looks like it should work. I hope it doesn't mess with the general layout of the button objects.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #8

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

            1 Reply Last reply
            0
            • SGaistS SGaist

              @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)

              C Offline
              C Offline
              CraigBakalian
              wrote on last edited by
              #9

              @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?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                alex_malyu
                wrote on last edited by alex_malyu
                #10

                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();
                
                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rondog
                  wrote on last edited by Rondog
                  #11

                  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.

                  1 Reply Last reply
                  1
                  • A Offline
                    A Offline
                    alex_malyu
                    wrote on last edited by
                    #12

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

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Rondog
                      wrote on last edited by
                      #13

                      I am pretty sure I tried this and found it didn't work (for QMessageBox at least). It was quite a long time ago so maybe it is different now.

                      1 Reply Last reply
                      0
                      • Pl45m4P Pl45m4 referenced this topic on

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved