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. Whats the best solution?
Qt 6.11 is out! See what's new in the release blog

Whats the best solution?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 520 Views 1 Watching
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    I'm looking at some old code which tests for a single character in a QString, currently there are a lot if else tests with a single line in the body of the if:

        if ( strSymbol== "~" ) {
            ui->lnedField->setText(ui->text() + "~");
        } else if ( strSymbol == "!") {
            ui->lnedField->setText(ui->text() + "!");
        } ....
    

    There is a total of about 40 if else's, is it more efficient to do this or to create a single array (string) of all characters to test then use:

        QString strChList = "~!@#$%^& ....
        
        if ( strChList.indexOf(strTest) != -1 ) {
            QString strCurrent(ui->lnedField->text());
            strCurrent.append(strTest);
            ui->lnedField->setText(strCurrent);
        }
    

    Or is there another more optimal solution?

    Kind Regards,
    Sy

    jsulmJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I'm looking at some old code which tests for a single character in a QString, currently there are a lot if else tests with a single line in the body of the if:

          if ( strSymbol== "~" ) {
              ui->lnedField->setText(ui->text() + "~");
          } else if ( strSymbol == "!") {
              ui->lnedField->setText(ui->text() + "!");
          } ....
      

      There is a total of about 40 if else's, is it more efficient to do this or to create a single array (string) of all characters to test then use:

          QString strChList = "~!@#$%^& ....
          
          if ( strChList.indexOf(strTest) != -1 ) {
              QString strCurrent(ui->lnedField->text());
              strCurrent.append(strTest);
              ui->lnedField->setText(strCurrent);
          }
      

      Or is there another more optimal solution?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @SPlatten Sounds like a good idea, but make strChList const static

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #3

        @jsulm, Thank you

        On looking into this a it further its not quite as straight forward as I first thought....because the destination is a QLineEdit, we first have to get the content of the QLineEdit, unfortunately QLineEdit has no append function.

        Kind Regards,
        Sy

        1 Reply Last reply
        0

        • Login

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