Unable to move OK button in QMessageBox
-
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? -
@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();
-
@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.