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. <QValueAxis> setLabelFormat() padded with blank spaces
QtWS25 Last Chance

<QValueAxis> setLabelFormat() padded with blank spaces

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.8k Views
  • 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.
  • J Offline
    J Offline
    Johannis259
    wrote on last edited by Johannis259
    #1

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

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

    Does anyone know the right format string ?

    JonBJ 1 Reply Last reply
    0
    • J Johannis259

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

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

      Does anyone know the right format string ?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Johannis259
      You just want: %4d

      1 Reply Last reply
      1
      • J Offline
        J Offline
        Johannis259
        wrote on last edited by
        #3

        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.

        JonBJ 1 Reply Last reply
        0
        • J Johannis259

          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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

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

          1 Reply Last reply
          1
          • J Offline
            J Offline
            Johannis259
            wrote on last edited by
            #5

            Thanks for your suggestions.

            qDebug()<<QString::asprintf("%4d",129)
            result is " 129" so the format string is correct.

            Using a fixed font:
            valueAxisY->setLabelsFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
            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.

            1 Reply Last reply
            0
            • thomas.denicoloT Offline
              thomas.denicoloT Offline
              thomas.denicolo
              wrote on last edited by
              #6

              I also encounter this problem in Qml:

              ValueAxis{
                labelFormat: "%4d"
              }
              

              does not solve the problem.

              Has anyone managed to fix it somehow?

              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