S
S
Svyatoslav Nemato2019-03-05 15:38:48
Qt
Svyatoslav Nemato, 2019-03-05 15:38:48

How to write a universal function from repeated methods?

There is a large form with many fields, for each field I write the following code:

bool case_medical_care::eventFilter(QObject *target, QEvent *event)
{	
//ПОЛЕ 1
  if (target == ui->data)
    {
        if (event->type() == QEvent::FocusIn and dataFocus)
        {
             dataFocus = false;
             on_data_textEdited(ui-data->text());
        }
        if (event->type() == QEvent::FocusOut and !dataFocus)
        {
            dataFocus = true;
            ui->data->setCursorPosition(0);
        }
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Down or keyEvent->key() == Qt::Key_Up)
            {
                on_dataP_textEdited(ui->data->text());
            }
        }
    }
  
//ПОЛЕ 2
  if (target == ui->text)
    {
        if (event->type() == QEvent::FocusIn and textFocus)
        {
             textFocus = false;
             on_text_textEdited(ui-text->text());
        }
        if (event->type() == QEvent::FocusOut and !textFocus)
        {
            textFocus = true;
            ui->text->setCursorPosition(0);
        }
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Down or keyEvent->key() == Qt::Key_Up)
            {
                on_text_textEdited(ui->text->text());
            }
        }
    }
  return QObject::eventFilter(target, event);
}

Is it possible to make one function out of this code so that I would not have to change it, but I simply entered the name of the fields into some kind of array, for example, and passed it to it.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question