How can I pad a QString with spaces on the end?
For example, I want a QString to be 4 characters in total, and it’s only 1 character long. I would like the last 3 to be spaces.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Strangely enough, there’s a function for specifically that called
QString::leftJustifiedhttp://doc.qt.io/qt-4.8/qstring.html#leftJustified
So
paddedString = originalString.leftJustified(4, ' ');would do the trick.(Note that you can also optionally truncate the string if it’s longer than your character limit by passing in a third parameter of
true.)