QString Formatting - Lining up multiple QStrings
-
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 -
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 -
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 -
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!
-
@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);
@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)+"...";