C++ Qt5: Concatenating mixed types into a QString (string) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C++ Qt5: Concatenating mixed types into a QString (string)

I am learning to use Qt5 with C++ and I want the simplest way to concatenate variables amongst string literals. I have the following code: https://code.sololearn.com/cz5SQVdy9L1t/?ref=app 'this' is the MainWindow object and i have defined a few public variables in the header file: length and height as double type. verticalPoles is integer type. MAX_* are constant values used for the math. The if code block is intentionally empty, i'm just learning and trying this for now. With this code, the output is not correct. It concatenates the string literals together correctly, but the values of the double/integer types are not shown. What's the simplest way to concat mixed types together? I'm happy to use Qt specific functions or operators too if there's any Qt experts here.

4th Mar 2019, 3:28 AM
Nathan Stanley
Nathan Stanley - avatar
1 Answer
0
Ha I found the answer. Use the QString::arg function and daisy chain the .arg() calls onto the end of the QString variable name when you output it.: qsOutput = "Wall length of %1 metres will require %2 vertical poles."; ui->lblOutput->setText(qsOutput.arg(this->length).arg(verticalPoles));
4th Mar 2019, 3:45 AM
Nathan Stanley
Nathan Stanley - avatar