vendredi 31 août 2018

Generic code to align non-constant-width text on QLabel

I want to align text as follow on a QLabel:

name         : value
more         : other value
longer name  : value

It is easily done as follow

QString str;
str += "name\t\t: value\n";
str += "more\t\t: other value\n"
str += "longer name\t: value"

The question is, if I do not know the string pairs beforehand, is there a way in Qt to determine how many \t-chars must each string get so that they align on the : (if tabs are the only option)?

I can't use QString::arg() since the text is displayed on a GUI using non-constant-width-text. The issue here is that if I count the number of chars and set the fieldWidth the : does not align since the width of each char is not the same (changing the font is not an option)

I have tried some logic to count and then 'guess' the number of tabs to insert, which works in general but there might be some corner cases where it might not work.

Aucun commentaire:

Enregistrer un commentaire