Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Find length of QString text. [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Find length of QString text. [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 30.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    st2000
    wrote on 29 Jul 2011, 21:11 last edited by
    #1

    Hi...
    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?

    -thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      st2000
      wrote on 29 Jul 2011, 21:39 last edited by
      #2

      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;
      }
      }@

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Franzk
        wrote on 29 Jul 2011, 21:40 last edited by
        #3

        @QString murk = "huuhaa";
        int size = murk.size();
        int length = murk.length();
        // size == length
        @

        "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on 29 Jul 2011, 21:41 last edited by
          #4

          Please enclose code in @ blocks. Thanks!

          (And be sure to add a [SOLVED] to the title of the thread.)

          Edit: Ok, you did. Sorry! :-)

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tobias.hunger
            wrote on 18 Aug 2011, 18:09 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on 19 Aug 2011, 11:29 last edited by
              #6

              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...

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved