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. QString tabulator \t problem
Forum Updated to NodeBB v4.3 + New Features

QString tabulator \t problem

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 11.2k 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.
  • B Offline
    B Offline
    Binary91
    wrote on 30 Oct 2014, 10:45 last edited by
    #1

    Hi,
    I've got the following problem:
    I'm trying to display the name of a person and it's age in a QListWidget. For better look, I'd like to have the ages justified like in a chunk, so that it does not matter how long the name of the person is, the ages (numbers) are justified and begin exactly at the same point (x coord), Example:
    Name 1:-------10
    Name 11:------20
    Name 111:-----30

    To get this, I use a tabulator:
    @QString input = "Name 1:\t"+QString::number(age);@
    That works, maybe because QListWidget has exact coords for tabulator signs?!

    The problem is, I also try to display these names/ages in a QMessageBox, but for reasons I don't understand, the tabulator sign is relative to the length of the name:
    Name 1:-------10
    Name 11:------20
    Name 111:-----------------30

    Does anybody know, how I can handle that? Is there a way to calculate the count of tabulators needed in relation to the prior text? Or can I define a tabulator width as fixed like it is in QListWidget?

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jazzco
      wrote on 30 Oct 2014, 11:28 last edited by
      #2

      If you are using a monospace font (e.g. console) then you just need to count the characters and divide it with 8 (standard tab width). You need to go through the output twice, first to count your maximum tab needed, second to create the output with proper tab-count added.

      With a proportional font you need to ask your output device for the pixel-width.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Binary91
        wrote on 31 Oct 2014, 14:33 last edited by
        #3

        First of all, thank you for the support.
        [quote]With a proportional font you need to ask your output device for the pixel-width[/quote]I think this is my case, because the tab-count is different related to the character, example:
        iiiiiiiii --> one tab
        wwwwwwwww --> more tabs (logical)

        What do you mean with "ask your output device" ? How can I do that?

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jazzco
          wrote on 3 Nov 2014, 08:46 last edited by
          #4

          Ok, now we get a little deeper. To adapt my answer to your case it would be helpful to see a bit of your source code. Can you pass the lines, where your output is done?

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Binary91
            wrote on 3 Nov 2014, 09:10 last edited by
            #5

            Ah, never thought that this topic would get focus one more time.
            Sure, I have a table with a horizontal header. This header contains the names of persons.
            These persons are listed in a QListWidget with a corresponding sum (amount).
            @QStringList header, list;
            QString person, messagebox_text = "";
            header << "Person 2" << "Person 2" << "Person 3" << "Person 4" << "Person 5";
            foreach(person, header)
            {
            list << person+":\t0,00";
            messagebox_text += person+":\t0,00\n";
            }
            updateMyList(list); // every amount is correctly displayed, doesn't matter how long the name of the person is
            QMessageBox::information(0, "Overview", messagebox_text, QMessageBox::Ok); // tabulators differ according to person-name length@
            Well, that's it. I know there must be a solution, because QListWidget manages it the right way, only QMessageBox has problems with it...

            I hope you (or someone else) can help me with that.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jazzco
              wrote on 3 Nov 2014, 12:32 last edited by
              #6

              Ok, here is some code. By now, it is no sollution, because the width of the Tabulator is not calculated correctly. Maybe it helps as a base.

              @ QStringList list;
              QStringList list2;
              list << "Alf" << "Beatrix" << "Caesar" << "Dalmatiner" << "Ephraim Kishon";
              list2 << "0.3" << "1.4" << "0.7" << "3.3" << "2.1";
              QMessageBox mBox;
              QFontMetrics fm = mBox.fontMetrics();
              int tabWidth = fm.width("\t");
              qDebug() << "TabWidth"<<tabWidth;
              int maxWidth = 0;
              foreach (QString s, list) {
              maxWidth = qMax(maxWidth, fm.width(s+"\t"));
              qDebug() << maxWidth;
              }
              QStringList outList;
              for (int i = 0; i < list.size(); ++i) {
              QString s1 = list.at(i);
              QString s2 = list2.at(i);
              while (fm.boundingRect(s1+".").width() < maxWidth) s1 += "\t";
              s1.replace('\t','^'); // just to see the amount of TABs
              outList << (s1+s2);
              }
              mBox.setText(outList.join("\n"));
              mBox.exec();
              @

              The qDebug-messages always say the width is 80, which is wrong for that device.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jazzco
                wrote on 3 Nov 2014, 15:05 last edited by
                #7

                I browsed the Qt-Sources: the support of tabulators in Qt seams to be very spartanic up to non-existent.

                As a workaround maybe its better to define an own Dialog instead of using the default. Just put in two QLabel with vertical alignment and the "MinimumExpanding" SizePolicy for the right label.

                Or you can use html formatted text like this:

                @ QStringList list;
                QStringList list2;
                list << "Alf" << "Beatrix" << "Caesar" << "Dalmatiner" << "Ephraim Kishon";
                list2 << "0.3" << "1.4" << "0.7" << "3.3" << "2.1";
                QString html = "<html><table>%1</table></html>";
                for (int i = 0; i < list.size(); ++i) {
                QString row = "<tr><td>%1</td><td>+nbsp;%2</td></tr>"; // replace + by &
                row = row.arg(list.at(i)).arg(list2.at(i));
                html = html.arg(row+"%1");
                }
                html = html.arg("");
                QMessageBox::information(this, "Information", html, "Ok");
                @

                1 Reply Last reply
                0

                1/7

                30 Oct 2014, 10:45

                • Login

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