Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to position buttons on QMessageBox?



  • When execute this code, the buttons appears on the rigth.
    How can I center the buttons on QMessageBox?

    Captura.PNG

    QMessageBox msgBox;
    msgBox.setText("Detener proceso?");
    msgBox.setWindowFlags(Qt::FramelessWindowHint);
    QPushButton *yesButton = msgBox.addButton("Si", QMessageBox::ActionRole);
    QPushButton *noButton = msgBox.addButton("No", QMessageBox::ActionRole);
    msgBox.setStyleSheet("QMessageBox QPushButton{"
                         "font-family: Lato Heavy; "
                         "font-weight: bold; "
                         "font-size: 20pt; "
                         "background-color: rgb(50, 50, 50, 255);"
                         "color: white;"
                         "}");
    
    msgBox.setStyleSheet("QMessageBox QLabel{"
                         "color: black; "
                         "}");
    
    msgBox.setStyleSheet("QMessageBox{"
                         "font-family: Lato Heavy; "
                         "font-weight: bold; "
                         "font-size: 15pt; "
                         "background-color: rgb(200, 200, 200, 255);"
                         "}");
    msgBox.exec();
    if ((QPushButton*)msgBox.clickedButton() == yesButton) {
        return true;
    } else if ((QPushButton*)msgBox.clickedButton() == noButton) {
        return false;
    }
    

    Thanks.


  • Lifetime Qt Champion

    Hi,

    You can do simpler: QDialogButtonBox. That class does the work for you.


Log in to reply