I have three checkboxes, QFontComboBox and QSpinBox, as well as the main text field QTextEdit.

It is necessary to make SpinBox change the font size, checkboxes make the text italic, underlined and bold, respectively.

Checkboxes try to do this:

// чСкбоксы if (ItalyStyle->isChecked()) { MainTextEdit->setFontItalic(true); } if (BoldStyle->isChecked()) { MainTextEdit->setFontWeight(true); } if (UnderLineStyle->isChecked()) { MainTextEdit->setFontUnderline(true); } 

Does not work. If not difficult, help.

And another question - How to set the font format from QFontComboBox?

  • What signal do you hang all this on? Show the code - Alexander Chernin
  • Be sure to make a separate signal? Why then is the function isChecked ()? - Konstantin_SH
  • No, not necessarily, you can do with one for all checkboxes. How do you do it? - Alexander Chernin
  • That's how it is implemented. connect (ItalyStyle, SIGNAL (isChecked), SLOT (slotCheckBoxactive ())); - Konstantin_SH
  • isChecked is not a signal, it won’t work - Alexander Chernin

1 answer 1

The QCheckBox class has such a stateChanged(int state) signal.

If you want one handler for all checkboxes, do this:

 class MainClass : ... { Q_OBJECT //... private slots: void checkBoxChanged(int state); //.. } 

Implementation:

 MainClass::MainClass(QWidget* parent) : QMainWindow(parent) { // ... QTextEdit* MainTextEdit = new QTextEdit(this); // ! ΠžΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ Π½Π°Π΄ΠΎ Π·Π°Π΄Π°Ρ‚ΡŒ имя, ΠΈΠ½Π°Ρ‡Π΅ ΠΌΡ‹ Π΅Π³ΠΎ Π½Π΅ Π½Π°ΠΉΠ΄Π΅ΠΌ MainTextEdit->setObjectName("MainTextEdit"); connect(ItalyStyle, SIGNAL(stateChanged(int)), this, SLOT(checkBoxChanged(int))); connect(BoldStyle, SIGNAL(stateChanged(int)), this, SLOT(checkBoxChanged(int))); connect(UnderLineStyle, SIGNAL(stateChanged(int)), this, SLOT(checkBoxChanged(int))); // ... } void MainClass::checkBoxChanged(int state) { // Π˜Ρ‰Π΅ΠΌ Π½ΡƒΠΆΠ½Ρ‹ΠΉ Π²ΠΈΠ΄ΠΆΠ΅Ρ‚ ΠΏΠΎ Π΅Π³ΠΎ ΠΈΠΌΠ΅Π½ΠΈ QTextEdit* MainTextEdit = findChild<QTextEdit*>("MainTextEdit"); if( MainTextEdit != nullptr ) { // Π’Ρ‹Π±ΠΈΡ€Π°Π΅ΠΌ Π½ΡƒΠΆΠ½Ρ‹ΠΉ чСкбокс ΠΏΠΎ ΠΈΠΌΠ΅Π½ΠΈ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ if( sender()->objectName() == "ItalyStyle" ) MainTextEdit->setFontItalic(state == Qt::Checked); else if( sender()->objectName() == "BoldStyle" ) MainTextEdit->setFontWeight(state == Qt::Checked); else if( sender()->objectName() == "UnderLineStyle" ) MainTextEdit->setFontUnderline(state == Qt::Checked); } } 
  • If you make such a function, it says that ItalyStyle and other variables are not declared inside this function. - Konstantin_SH
  • And how does MainTextEdit "declare"? - Konstantin_SH
  • @Konstantin_SH in the constructor you need to add a string name to the widget and then see the slot - Alexander Chernin