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?
Asked
Active
Viewed 7,448 times
5

sabertooth1990
- 1,048
- 1
- 13
- 19
3 Answers
4
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.

sabertooth1990
- 1,048
- 1
- 13
- 19
0
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.

Silicomancer
- 8,604
- 10
- 63
- 130
-
True. But I wanted to use some class methods, as a cleaner way of doing stuff. – sabertooth1990 Mar 24 '14 at 11:39
-
1I don't think that one of those methods is cleaner than the other. But it is up to you which way to go. – Silicomancer Mar 24 '14 at 11:50
0
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

Qui-Gon Gym
- 1
- 1
- 3