Solved Clean way to break very long line
-
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?
-
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())));;
-
@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 takesint
,double
, etc. so you don't need to callQString::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 thestatic_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.