How to change the currentFont in a QTextEdit but leaving the formattation
-
I'm currently developing a notepad and I've found a problem. Basically in a QTextEdit I have different text with different point sizes. So if I want to change the font of these ones their point size will reset to the one of the textbox.
I will make an example with images.
before:
enter image description here
after:
As you can see I changed the font to times new roman but the point size changed.
This is the code:
void MainWindow::on_fontComboBox_currentFontChanged(const QFont &f) { ui->fontComboBox->setFont(f); QTextEdit *edit = getTabTextEdit(); if(ui->tabWidget->currentWidget() == ui->tab_1) { ui->edit->setCurrentFont(f); } else { edit->setCurrentFont(f); } }
How can I solve this?
-
Hi,
How did you create the "f" QFont object ?
-
That does not answer the question which is how is that font object created ?
From the looks of it, you are not just changing the font name but you are nuking the current one.
What is usually done is grabbing the current font of an object, modify whatever property you want and then apply that modified font on the widget.
-
QFont font = widget->font(); font.setFamily("something"); widget->setFont(font);
-
You should not apply fonts blindingly on QTextEdit if you want to have different fonts there you need to handle that à the document level.
-
You are using QTextEdit for custom editing, hence you want to only apply the font to the selected text and not the whole widget.