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. Unable to move OK button in QMessageBox
Forum Updated to NodeBB v4.3 + New Features

Unable to move OK button in QMessageBox

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 385 Views 1 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.
  • V Offline
    V Offline
    vijaychsk
    wrote on last edited by vijaychsk
    #1

    Hi,
    I wanted a QMessageBox with some scrollable text. I used QPlainTextEdit to create the text in a scrollable fashion and added as widget to the QMessageBox. It works but the OK button always comes to the top. I'm unable to add QPlainTextEdit before the OK button. Spacing is a different issue. I have a workaround for it.

    This is my code:

    QPlainTextEdit* plainText = new QPlainTextEdit(this);
    plainText->setReadOnly(true);
    plainText->setPlainText(QString::fromStdString(message));
    
    QMessageBox msgBox(this);
    msgBox.layout()->addWidget(plainText);
    msgBox.setIcon(QMessageBox::Information);
    msgBox.setStandardButtons(QMessageBox::Ok);
    msgBox.setDefaultButton(QMessageBox::Ok);
    msgBox.layout()->addWidget(plainText);
    msgBox.exec();
    

    Any tips on how to fix this? I tried the Qt documentation for QMessageBox but with no luck.
    Or
    Do you suggest any other better way to achieve this?

    81383018-0c8f-4530-82d9-412354644c97-image.png

    eyllanescE 1 Reply Last reply
    0
    • V vijaychsk

      Hi,
      I wanted a QMessageBox with some scrollable text. I used QPlainTextEdit to create the text in a scrollable fashion and added as widget to the QMessageBox. It works but the OK button always comes to the top. I'm unable to add QPlainTextEdit before the OK button. Spacing is a different issue. I have a workaround for it.

      This is my code:

      QPlainTextEdit* plainText = new QPlainTextEdit(this);
      plainText->setReadOnly(true);
      plainText->setPlainText(QString::fromStdString(message));
      
      QMessageBox msgBox(this);
      msgBox.layout()->addWidget(plainText);
      msgBox.setIcon(QMessageBox::Information);
      msgBox.setStandardButtons(QMessageBox::Ok);
      msgBox.setDefaultButton(QMessageBox::Ok);
      msgBox.layout()->addWidget(plainText);
      msgBox.exec();
      

      Any tips on how to fix this? I tried the Qt documentation for QMessageBox but with no luck.
      Or
      Do you suggest any other better way to achieve this?

      81383018-0c8f-4530-82d9-412354644c97-image.png

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @vijaychsk What you want is not preset so a trick must be used, in this case remove the QDialogButtonBox, insert the QPlainTextEdit and then the QDialogButtonBox.

          QString message{"Qt is awesome!!!"};
          QPlainTextEdit* plainText = new QPlainTextEdit;
          plainText->setReadOnly(true);
          plainText->setPlainText(message);
      
          QMessageBox msgBox(this);
          msgBox.setIcon(QMessageBox::Information);
          msgBox.setStandardButtons(QMessageBox::Ok);
          msgBox.setDefaultButton(QMessageBox::Ok);
          msgBox.show();
          QGridLayout *gridLayout = qobject_cast<QGridLayout *>(msgBox.layout());
          QDialogButtonBox *buttonBox = msgBox.findChild<QDialogButtonBox *>("qt_msgbox_buttonbox");
          if(gridLayout && buttonBox){
              int index = gridLayout->indexOf(buttonBox);
              int row, column, rowSpan, columnSpan;
              gridLayout->getItemPosition(index, &row, &column, &rowSpan, &columnSpan);
              buttonBox->setParent(nullptr);
              gridLayout->addWidget(plainText, row, column, rowSpan, columnSpan);
              gridLayout->addWidget(buttonBox, row + 1, column, rowSpan, columnSpan);
          }
          msgBox.exec();
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vijaychsk
        wrote on last edited by
        #3

        @eyllanesc Awesome! It works great. Thank you very much.
        After looking into various posts, it appears that QMessageBox is always very limited in nature. We need to do some tricks like these.

        Pl45m4P 1 Reply Last reply
        0
        • V vijaychsk

          @eyllanesc Awesome! It works great. Thank you very much.
          After looking into various posts, it appears that QMessageBox is always very limited in nature. We need to do some tricks like these.

          Pl45m4P Online
          Pl45m4P Online
          Pl45m4
          wrote on last edited by
          #4

          @vijaychsk

          Then maybe try a QDialog instead of a QMessageBox


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0

          • Login

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