Important: Please read the Qt Code of Conduct -

How to set StyleSheet for an specific label in QMessageBox?

  • I Want to have a bigger QMessageBox and centered texts in it but when I increase the size of it by stylsheet it'll be like this:

    alt text

    if I could give it Some Padding or margin it would be fixed but I can't.

    void MainWindow::showMsg()
        QMessageBox m_MsgBox;
        m_MsgBox.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
        m_MsgBox.setText("Your Trial is finished! Please purachase a plan.");
        m_MsgBox.setStyleSheet("QLabel{min-width:200 px; font-size: 13px;} QPushButton{ width:25px; font-size: 13px; }");
        if(m_MsgBox.exec() == QMessageBox::Ok)

    I want to give different css properties to each QLabel(QMessageBox::Warning & setText) in this QMessageBox.

    Any help will be appreciated. Thanks!

  • @Muhammad-Mirab-Br said in How to set StyleSheet for an specific label in QMessageBox?:

    min-width:200 px;

    Probably not an issue, but if you are having a problem why do you put a space into 200px in case that is not acceptable to QSS? Why make it different from all your other XXpx directives?

  • Sorry, I forgot to share the photo of it. I have problem with that the QMessageBox::Warning is having 200px width and It's not what I expected, I want to specify different QSS for these labels(QMessageBox::Warning, QLabel).
    alt text

  • Only answering to your topic title, if you look into the source code of QMessageBox, every label has a object name, so that should be easy to set different style to them by using ID selector.

    • text: "qt_msgbox_label"

    • icon: "qt_msgboxex_icon_label"

    • informativeText: "qt_msgbox_informativelabel"

    Note: These names may change in future versions.

  • @Bonnie Thanks! This really helped. This is what I was searching for.

  • @Bonnie How can I Make the button center align in QMessagebox? I tried: move(), setGeometry() and etc.

    At all, I want to make the button center. Thanks.

  • @Muhammad-Mirab-Br
    Option 1: find QDialogButtonBox and set property

    m_MsgBox.findChild<QDialogButtonBox*>(QString(), Qt::FindDirectChildrenOnly)->setCenterButtons(true);

    Option 2: set property by stylesheet:

    QDialogButtonBox { qproperty-centerButtons: true; }

  • Thanks @Bonnie, You are AWESOME!