Important: Please read the Qt Code of Conduct -

Center Text and Buttons in QMessageBox

  • Hi,
    I'm fairly new using the QMessageBox class and have a question about how to align the text and buttons. I'm using the following code to create a message box and would like the text and box to be centered but when the box is created the text and button are displayed on the right-hand side of the box. I added the html center code but that didn't help with the text. Not sure what to do about the button. Any ideas? Maybe I shouldn't be using QMessageBox? I've searched through the QMessageBox class description and can't find anything on alignment.

    QMessageBox msgBox;
    msgBox.setText("<center>Machine Configuration Saved</center>");
    msgBox.setWindowTitle("System Configurator");

  • Hi,
    @msgBox.setText("<p align='center'>First Line<br>"
    "Second Line<br>"
    "Thirth Line</p>");@

  • Wether the buttons are centered is determined from the current style and is therefore platform dependend.

    The text can be centered as shown by mohsen.

    The icon is on the left side and has always reserved some space so the text is not completely aligned in the center as there's some phantom icon on the left (that may be changed by explicitly setting a 0x0 size icon, I did not test it).

    So, in your case, I would go with a handcrafted message box (use designer!) with a label for the title, another label for the text and the buttons in a layout.

Log in to reply