Find length of QString text. [SOLVED]
This could be one of those "I'm going about this wrong" things. So, as you read - speak up if there is a (completely) different way to approach this problem.
I would like to lay out a grid of buttons. The text is from a SQL data base. I would like to use a grid of 3 by 3 buttons. But some times there is so much text I can't fit everything into such a small button. So then I would like to use a 2 by 3 grid of buttons. Or even a 1 by 3 grid should the button labels be really long.
I'm thinking if I check all my QStrings for length and find the longest one, I can make a smart button grid layout choice.
Trouble is, the string is of type QString and I have been hunting all around for something like QString->size but have yet to find it.
I know there is something I'm missing here. Anyone know what it is?
Oh, I'm an idiot.
Here's the solution in case someone stumbles onto this thread:
I've an array of buttons of type QPushButton.
I suspect the type of buttons[which_button]->text is QString.
Ok, so if we look up QString we see there is a public function called size.
So, we can do something like this:
@int max_string_length, which_button, this_string_length;
max_string_length = 0;
for(which_button = 0; which_button < 10; which_button++)
this_string_length = buttons[which_button]->text().size(); // Wow.
if(this_string_length > max_string_length);
max_string_length = this_string_length;
@QString murk = "huuhaa";
int size = murk.size();
int length = murk.length();
// size == length
Please enclose code in @ blocks. Thanks!
(And be sure to add a [SOLVED] to the title of the thread.)
Edit: Ok, you did. Sorry! :-)
Note that QString::count() do return the number of characters in the string. This is not necessarily related to the number of glyphs (things painted aka. what a normal user thinks of as a character). Due to the wonders of unicode the QString containing 'Ä' might have a count of 1 or 2:-)
The QString::count() is also not necessarily related to the number of pixels needed to render the string.
E.g. the representation of "iiiii" is probably shorter than the representation of "MMMM", even which it having 5 instead of 4 letters (of course depending on the font used). You can use QFontMetrics to find the size in pixels a rendering of a QString will take. I think for your use case that would be the a best thing to do.
I am just wondering what you are trying to achieve at all. To me grids of buttons filled from a database bring back scary memories of - dare I say it? - calendars. Perhaps a button is not what you need for your UI at all...