QString Formatting - Lining up multiple QStrings
-
wrote on 28 Feb 2022, 14:50 last edited by
Hello,
I am currently trying to line up text within a QListWidget so that the temperature, percentage and status are always on the same vertical line.
Example of what I'm aiming for
I am using this line of code to format each row, which as you can see doesn't quite line up properly.
QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -16, ' ').arg(cpuTemp + "C", -4, ' ').arg(cpuUsage + "%", -4, ' ').arg(isOnline);
Does anyone have any suggestions on how to perfectly align these rows?
Hostname: Max 18 characters
Temperature: Max 3 characters
Usage: Max 3 characters
Status: Max 1 characterAny help would be greatly appreciated!
Thanks,
Ryan -
Hello,
I am currently trying to line up text within a QListWidget so that the temperature, percentage and status are always on the same vertical line.
Example of what I'm aiming for
What I get
I am using this line of code to format each row, which as you can see doesn't quite line up properly.
QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -16, ' ').arg(cpuTemp + "C", -4, ' ').arg(cpuUsage + "%", -4, ' ').arg(isOnline);
Does anyone have any suggestions on how to perfectly align these rows?
Hostname: Max 18 characters
Temperature: Max 3 characters
Usage: Max 3 characters
Status: Max 1 characterAny help would be greatly appreciated!
Thanks,
Ryanwrote on 28 Feb 2022, 14:55 last edited by KroMignon@RyanAR said in QString Formatting - Lining up multiple QStrings:
Any help would be greatly appreciated!
If you want to use spaces to align text, you also have to use a monospaced font and not a proportional font.
-
Hello,
I am currently trying to line up text within a QListWidget so that the temperature, percentage and status are always on the same vertical line.
Example of what I'm aiming for
What I get
I am using this line of code to format each row, which as you can see doesn't quite line up properly.
QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -16, ' ').arg(cpuTemp + "C", -4, ' ').arg(cpuUsage + "%", -4, ' ').arg(isOnline);
Does anyone have any suggestions on how to perfectly align these rows?
Hostname: Max 18 characters
Temperature: Max 3 characters
Usage: Max 3 characters
Status: Max 1 characterAny help would be greatly appreciated!
Thanks,
Ryan@RyanAR if you want to use String formatting to align those columns, than you have to use a Monospaced font, so that each character has the same width.
alternatively, what I would suggest, using a QListView and defining your own Delegate, that shows the data.
That way you can use QFontMetrics to get the max width of each text in one column, and position the texts accordingly -
wrote on 28 Feb 2022, 15:20 last edited by
Can't believe it was that simple!
Added "font-family:courier;" to the CSS of the QListWidget.
Thanks for the help @KroMignon @J-Hilk!
-
Can't believe it was that simple!
Added "font-family:courier;" to the CSS of the QListWidget.
Thanks for the help @KroMignon @J-Hilk!
-
Can't believe it was that simple!
Added "font-family:courier;" to the CSS of the QListWidget.
Thanks for the help @KroMignon @J-Hilk!
wrote on 28 Feb 2022, 15:25 last edited by@RyanAR said in QString Formatting - Lining up multiple QStrings:
Can't believe it was that simple!
You'r welcome... Sometimes solution is so evident that we becomes blind and can not see it ;)
-
wrote on 28 Feb 2022, 15:56 last edited by
@JonB - I did a brave! Thanks for the suggestion. Hostname is left justified, and the rest are now right justified, so all the symbols match up in all scenarios. I also added a max character limit on the hostname to replace trailing characters with ".." if the name is too large.
if(hostname.length() > 15) { hostname.chop(hostname.length() - 15); hostname = hostname + ".."; }
QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -18, ' ').arg(cpuTemp + "C", 4, ' ').arg(cpuUsage + "%", 4, ' ').arg(isOnline);
-
@JonB - I did a brave! Thanks for the suggestion. Hostname is left justified, and the rest are now right justified, so all the symbols match up in all scenarios. I also added a max character limit on the hostname to replace trailing characters with ".." if the name is too large.
if(hostname.length() > 15) { hostname.chop(hostname.length() - 15); hostname = hostname + ".."; }
QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -18, ' ').arg(cpuTemp + "C", 4, ' ').arg(cpuUsage + "%", 4, ' ').arg(isOnline);
-
@JonB - I did a brave! Thanks for the suggestion. Hostname is left justified, and the rest are now right justified, so all the symbols match up in all scenarios. I also added a max character limit on the hostname to replace trailing characters with ".." if the name is too large.
if(hostname.length() > 15) { hostname.chop(hostname.length() - 15); hostname = hostname + ".."; }
QString finalListItem = QString("%1 %2 %3 %4").arg(hostname, -18, ' ').arg(cpuTemp + "C", 4, ' ').arg(cpuUsage + "%", 4, ' ').arg(isOnline);
wrote on 28 Feb 2022, 16:15 last edited by@RyanAR said in QString Formatting - Lining up multiple QStrings:
if(hostname.length() > 15)
{
hostname.chop(hostname.length() - 15);
hostname = hostname + "..";
}Looks strange to me... I would change this to:
if(hostname.length() > 18) // as 18 is the max expected length hostname = hostname.left(15)+"...";
1/9