Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to refactor this if else code containing object constructors?
Forum Update on Monday, May 27th 2025

How to refactor this if else code containing object constructors?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 289 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Guerrian
    wrote on 26 May 2018, 14:17 last edited by
    #1

    QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
    {
    QLineEdit *lineEdit = new QLineEdit(parent);
    if (index.column() == 0)
    {
    QRegularExpression regExp(REG_EXP1);
    QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
    lineEdit->setValidator(validator);
    }
    else
    {
    QRegularExpression regExp(REG_EXP2);
    QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
    lineEdit->setValidator(validator);
    }
    return lineEdit;
    }

    There must be a way of refactoring this so that it is shorter.

    Linux Mint 18.3
    Qt 5.14.1
    Qt Creator 4.11.1

    P 1 Reply Last reply 26 May 2018, 14:35
    0
    • G Guerrian
      26 May 2018, 14:17

      QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
      {
      QLineEdit *lineEdit = new QLineEdit(parent);
      if (index.column() == 0)
      {
      QRegularExpression regExp(REG_EXP1);
      QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
      lineEdit->setValidator(validator);
      }
      else
      {
      QRegularExpression regExp(REG_EXP2);
      QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
      lineEdit->setValidator(validator);
      }
      return lineEdit;
      }

      There must be a way of refactoring this so that it is shorter.

      P Offline
      P Offline
      Paul Colby
      wrote on 26 May 2018, 14:35 last edited by Paul Colby
      #2

      Hi @Guerrian,

      There must be a way of refactoring this so that it is shorter.

      Well, shorter is not necessarily always best (eg when it reduces readability), but in this case, I'd write it like:

      QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
      {
          QRegularExpression regExp((index.column() == 0) ? REG_EXP1 : REG_EXP2);
          QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, parent);
          QLineEdit * const lineEdit = new QLineEdit(parent);
          lineEdit->setValidator(validator);
          return lineEdit;
      }
      

      or possibly:

      QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
          QRegularExpression regExp((index.column() == 0) ? REG_EXP1 : REG_EXP2);
          QLineEdit *lineEdit = new QLineEdit(parent);
          lineEdit->setValidator(new QRegularExpressionValidator(regExp, parent));
          return lineEdit;
      }
      

      It is possible to make it even shorter, but I think readability begins to be impacted then.

      Cheers.

      1 Reply Last reply
      3

      1/2

      26 May 2018, 14:17

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved