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.
1 answer
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.
|

QFontuseful, I guess. Link - vegorovQComboBox,QCheckBox,QSpinBox, and on the left is an element for editing the text, I don’t remember what it is called, IQTextEdit. ItstextDocumentproperty hasdefaultFontproperty - vegorovQGroupBox- it also has a frame and atitle(parameters on the screenshot) - vegorov