Important: Please read the Qt Code of Conduct -

QString tabulator \t problem

  • 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?

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

  • 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?

  • 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?

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

  • 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 =;
    QString s2 =;
    while (fm.boundingRect(s1+".").width() < maxWidth) s1 += "\t";
    s1.replace('\t','^'); // just to see the amount of TABs
    outList << (s1+s2);

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

  • 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(;
    html = html.arg(row+"%1");
    html = html.arg("");
    QMessageBox::information(this, "Information", html, "Ok");

Log in to reply