Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

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 style 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>",
)

Results in: QMessageBox

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top