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. Clean way to break very long line
Forum Updated to NodeBB v4.3 + New Features

Clean way to break very long line

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 1.2k Views 2 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.
  • E Offline
    E Offline
    Eligijus
    wrote on last edited by
    #1

    What is the best way to break long line like this:

    ui->buttonTracer2->setText(QString("x: %1 \n y: %2").arg(QString::number(static_cast<double>(convert.SamplesToRangemm(myTracer->position->key(), parse.getPulse().DS.at(0), parse.getFixed().GI))/1000000), QString::number(myTracer->position->value())));
    

    or should i just put it in another function and assign function calls to variables?

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by JKSH
      #2

      well
      one way is to use variables
      auto tracer_key=myTracer->position->key();
      auto Pulse = parse.getPulse().DS.at(0);
      auto Fixed = parse.getFixed().GI));
      etc
      ui->buttonTracer2->setText(QString("x: %1 \n y: %2").arg(QString::number(static_cast<double>(convert.SamplesToRangemm(tracer_key, Pulse, Fixed/1000000), QString::number(myTracer->position->value())));

      that also allows for check the values before use.

      update: yes. i would but in function./ use variables :)

      Also just breaking it , makes it more readable. ( IMHO)

      ui->buttonTracer2->setText(
          QString("x: %1 \n y: %2").arg(
          QString::number(static_cast<double>(convert.SamplesToRangemm(myTracer->position->key(), 
          parse.getPulse().DS.at(0), parse.getFixed().GI)) / 1000000), 
          QString::number(myTracer->position->value())));;
      
      JKSHJ 1 Reply Last reply
      6
      • mrjjM mrjj

        well
        one way is to use variables
        auto tracer_key=myTracer->position->key();
        auto Pulse = parse.getPulse().DS.at(0);
        auto Fixed = parse.getFixed().GI));
        etc
        ui->buttonTracer2->setText(QString("x: %1 \n y: %2").arg(QString::number(static_cast<double>(convert.SamplesToRangemm(tracer_key, Pulse, Fixed/1000000), QString::number(myTracer->position->value())));

        that also allows for check the values before use.

        update: yes. i would but in function./ use variables :)

        Also just breaking it , makes it more readable. ( IMHO)

        ui->buttonTracer2->setText(
            QString("x: %1 \n y: %2").arg(
            QString::number(static_cast<double>(convert.SamplesToRangemm(myTracer->position->key(), 
            parse.getPulse().DS.at(0), parse.getFixed().GI)) / 1000000), 
            QString::number(myTracer->position->value())));;
        
        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @mrjj said in Clean way to break very long line:

        Also just breaking it , makes it more readable. ( IMHO)

        +1

        You can use multiple levels of nesting to break:

        ui->buttonTracer2->setText(
                QString("x: %1 \n y: %2").arg(
                        QString::number(
                                static_cast<double>(
                                        convert.SamplesToRangemm(
                                                myTracer->position->key(),
                                                parse.getPulse().DS.at(0),
                                                parse.getFixed().GI
                                        )
                                ) / 1000000
                        ),
                        QString::number(myTracer->position->value())
                )
        );
        

        You can mix intermediate variables with breaks:

        auto xStr = QString::number(
                static_cast<double>(
                        convert.SamplesToRangemm(
                                myTracer->position->key(),
                                parse.getPulse().DS.at(0),
                                parse.getFixed().GI
                        )
                ) / 1000000
        );
        
        auto yStr = QString::number(myTracer->position->value());
        
        ui->buttonTracer2->setText(
                QString("x: %1 \n y: %2").arg(xStr, yStr)
        );
        

        Alternatively, your final line can use string concatenation instead of replacement via arg():

        ui->buttonTracer2->setText("x: " + xStr + "\n y: " + yStr);
        

        Finally, you can simplify your code. Note that:

        • QString::arg() has overloads that takes int, double, etc. so you don't need to call QString::number()
        • You can implicitly cast the return value of SamplestoRangemm() into a double by dividing it with a double literal, so you don't need the static_cast
        auto xVal = convert.SamplesToRangemm(
                myTracer->position->key(),
                parse.getPulse().DS.at(0),
                parse.getFixed().GI
        ) / 1000000.0;
        // The compiler treats "1000000.0" as a double, so it implicitly casts your result to a double
        
        auto yVal = myTracer->position->value();
        
        ui->buttonTracer2->setText(
                QString("x: %1 \n y: %2").arg(xVal).arg(yVal)
        );
        

        NOTE: What's the "best" way to indent lines, or whether an implicit cast is "better" than static_cast, are discussion topics for another day.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        4

        • Login

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