Unsolved Qt QMessageBox get rid of label left margin
-
I want to display some information message, for that I decided to use
QMessageBox
with no icon, problem is there is a huge gap between left part of widget and text, tried to remove it by setting margins and spacings to 0, but everything was removed except that left part.Here is a simple example of what I have.
QMessageBox messageBox(QMessageBox::NoIcon, "My Title", "This is my text that I want to display.\nBut there is annoying left margin that I can't remove whatever I try.\n", QMessageBox::StandardButton::Ok); messageBox.exec();
Any ideas how to do that, or I should create my own simple widget?
-
Hi
well you could do likevoid MainWindow::on_pushButton_4_clicked() { QMessageBox messageBox(QMessageBox::Warning, "My Title", "This is my text that I want to display.\nBut there is annoying left margin that I can't remove whatever I try.\n", QMessageBox::StandardButton::Ok); auto labelIcon = messageBox.findChild< QLabel *>("qt_msgboxex_icon_label"); if (labelIcon) { labelIcon->setMaximumSize(QSize(0, 0)); } auto layout = messageBox.findChild< QGridLayout *>(); if (layout ) { layout->setSpacing(0); layout->setContentsMargins(0, 0, 12, 0); } messageBox.exec(); }
and get this.
do note we find internal widgets and fiddle with them. this might break in later Qt version if the rename the labels.
That said. Not so likely to ever happen. -
@mrjj hack may work, or it might not. It bypasses the established interface for the "canned widget" you are attempting to use. If you want total control and forward compatibility then design your own message box widget.
-
Well, it is indeed a hack.
I totally agree. If the poster wants the best solution, making a custom dialog is the way to go.
Also in his case, he doesn't want the icons, etc so really no gain in using the build-in messageBox.
But it worked for me in win/linux from 5.5 -> 5.15.2 so the risk seems very minor.