简体   繁体   中英

How to change font of a QMessageBox in Qt?

I was trying to build a simple application with a QComboBox and a QPushButton . The idea is to populate the QComboBox with a list of all available fonts in the system. When the user selects a font and presses the QPushButton then a QMessageBox appears with the font selected. Now how to do it?

The solution is using the setFont() method of the QMessageBox

QMessageBox *msg = new QMessageBox(QMessageBox::Information, "Message with font",
                         "This message is in font: " + ui->comboBox->currentText(),
                          QMessageBox::Ok | QMessageBox::Cancel, this);
QFont font = QFont(ui->comboBox->currentText());
msg->setFont(font);
msg->exec();

Where combobox is QComboBox used.

You can use basic HTML markups when setting the text to your message box label. The markup supported by QLabel includes <font> .This method also allows more versatile formatting.

As before suggested you could use styles in your Html blocks (in my example add styl to the paragraphs):

QMessageBox.about(
    self,
    "About",
    "<font>"
    "<p style='font-family: Terminal'>An simple app.</p>"
    "<p style='font-family: Georgia, 'Times New Roman'>- PyQt</p>"
    "<p>- Qt Designer</p>"
    "<p>- Python3</p>",
)

Reults: QMessageBox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM