January 27, 2011

retro_code retro_code
Lab Rat
9 posts

Center Text and Buttons in QMessageBox


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.

  2.     QMessageBox msgBox;
  3.     msgBox.setText("<center>Machine Configuration Saved</center>");
  4.     msgBox.setWindowTitle("System Configurator");
  5.     msgBox.addButton(QMessageBox::Ok);
  6.     msgBox.exec();

2 replies

January 27, 2011

Mohsen Mohsen
Lab Rat
685 posts


  1. msgBox.setText("<p align='center'>First Line<br>"
  2.                     "Second Line<br>"
  3.                     "Thirth Line</p>");



January 27, 2011

Volker Volker
Ant Farmer
5331 posts

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 0×0 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.

  ‹‹ Cross Platform Privilege Elevation programattically      remove dotted border around combo box items ››

You must log in to post a reply. Not a member yet? Register here!