Qt World Summit: Register Today!

<QValueAxis> setLabelFormat() padded with blank spaces

  • Hi, I would like to have a fixed size label with 4 characters in a QChart. If the value to be printed is shorter than 4, the result should be padded with blank spaces.

    Documentation for QValueAxis setLabelFormat(const QString &format) says:

    The format string supports the following conversion specifiers, length modifiers, and flags provided by printf() in the standard C++ library: d, i, o, x, X, f, F, e, E, g, G, c.

    I cannot figure out what is the correct format string.

    valueAxisY->setLabelFormat("%4.4d") gives 4 characters padded by zeros.

    I would like to have the same format but instead of zeros spaces.

    Does anyone know the right format string ?

  • @Johannis259
    You just want: %4d

  • Already tried that, but that does not seem to work.

    I have two QCharts (derived form the same class). They are identical except that in the first one the values range from 0 to 750 (3 digits) and in the second one the values range from 0 to 4000 (4 digits). They are each on a separate tab from a QTabWidget. If I switch between the two tabs, the size of the plot area size (the centre of the chart) changes. This is not the desired behaviour. The size of the plot area should be the same for both charts. It is when I use "%4.4d". Obviously because 0-750 becomes 0000-0750 and 0-4000 becomes 0000-4000. If I use %4d the horizontal size of the plot area increases/decreases when I switch between the tabs. It looks like that there are no spaces in front of the digits, so I assumed that I was not using the right format string.

    If there is another solution to achieve that the plot area of the QChart keeps the same size, that is also good.

  • @Johannis259
    Since %4d should deliver the correctly leading-space-padded number (you can verify this by trying it with https://doc.qt.io/qt-5/qstring.html#asprintf ?), maybe your problem is that you are displaying a proportional font so space is a lot thinner than, say 0, or setLabelFormat() ignores leading/trailing spaces? Could you play with http://doc.qt.io/archives/qt-5.7/qabstractaxis.html#labelsFont-prop to see if maybe a fixed font helps?

  • Thanks for your suggestions.

    result is " 129" so the format string is correct.

    Using a fixed font:
    does not make a difference. The plot area still resizes.

    When I hide the values using valueAxisY->hide(); the plot area remains the same size, so the problem is most likely caused by the printed values.

    It looks like that setLabelFormat() indeed ignores leading spaces.

  • I also encounter this problem in Qml:

      labelFormat: "%4d"

    does not solve the problem.

    Has anyone managed to fix it somehow?

Log in to reply