I make a simple text editor following the example from the training manual. I need to be able to make text of different sizes 14-12 pt and so on, as well as change the font Times New Roman, Arial and so on. What elements for this can be used? The rest of the functionality seems to be implemented. I attach a screenshot of the finished application to the question.

enter image description here .

  • one
    QFont useful, I guess. Link - vegorov
  • one
    Well, if by elements you meant controls - then on the right there is QComboBox , QCheckBox , QSpinBox , and on the left is an element for editing the text, I don’t remember what it is called, I QTextEdit . Its textDocument property has defaultFont property - vegorov
  • one
    Well, the panel itself on the right - apparently QGroupBox - it also has a frame and a title (parameters on the screenshot) - vegorov
  • doc.qt.io/qt-5/qtwidgets-richtext-textedit-example.html look here, this example is more scaled, but if you figure it out there will be a buzz - goldstar_labs 1:49 pm

1 answer 1

I would hang on the signal valueChanged() (the signals may be differently named, this is just an example) of the fontComboBox , sizeSpinBox and checkboxes of italics, bold and underlined one slot, which rereads the current values ​​and sets a new font for QTextEdit . It looks like this:

 MainWindow::MainWindow(QObject *parent): QMainWindow(parent) { setupUi(ui); connect(ui->fontComboBox, &QComboBox::currentIndexChanged, this, &MainWindow::updateFont); connect(ui->italicCheckBox, &QComboBox::stateChanged, this, &MainWindow::updateFont); // соединяем остальные } void MainWindow::updateFont() { QFont currentFont = ui->textEdit->currentFont(); currentFont->setFamily(ui->fontComboBox->currentText()); currentFont->setItalic(ui->italicCheckBox->isChecked()); currentFont->setPointSize(ui->sizeSpinBox->value()); // устанавливаем остальные свойства textEdit->setCurrentFont(currentFont); } 

Or, alternatively, you can hang on each element of the form, which is responsible for changing the font, in its slot, like this:

 void MainWindow::updateItalic() { QFont font = ui->textEdit->currentFont(); font->setItalic(ui->italicCheckBox->isChecked()); ui->textEdit->setCurrentFont(font); } // аналогично для семейства, размера и других чекбоксов 

But in this case, the question arises about the feasibility of creating a heap of slots, if the number of processed elements on the form will increase.