Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Automatically shrink text in a table like structure to fit widget size

    General and Desktop
    3
    8
    126
    Loading More Posts
    • 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.
    • gde23
      gde23 last edited by

      Hello,

      I've following problem. I have a widget that has some data in form of a table on it.
      At the moment its just a QGridLayout with QLabels on it, but it really does not matter how it is implemented if there is a better way.

      Now I want the widget to behave in a way, that if the widget size is getting too small for the text, the textsize will be reduced so it fits to the widget again.

      I've found some examples with looping around and measuring with QFontMetrics and so on, however all these approaches seem kind of extremely complicated for just having a table that will scale the text to fit.

      So is there a easy way to achieve such a basic behavior?

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @gde23 last edited by

        @gde23
        I believe that the examples are as they are, measuring font metrics, because that's what you will have to do. So far as I know, Qt is not going to change to a reduced-size text font to make anything fit.

        1 Reply Last reply Reply Quote 1
        • gde23
          gde23 last edited by

          @JonB thanks for the reply. The problem in my case is, that if i e.g. do the font metrics for all the labels this leads to some oscillating of the widgets since they trigger each others resize events and keep going forever.
          So this is not really an option.
          Would somethink like a graphics szene maybe work where the whole widget is rendered first so everything fits and then the rendered image is scaled down to the widgets space??

          I even had some opengl widget for the task which was kind of worked, but its really a stupid solution and there must be some better way to get this running,

          JonB 1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi,

            Why not use a QTableView for that ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 1
            • JonB
              JonB @gde23 last edited by

              @gde23 said in Automatically shrink text in a table like structure to fit widget size:

              @JonB thanks for the reply. The problem in my case is, that if i e.g. do the font metrics for all the labels this leads to some oscillating of the widgets since they trigger each others resize events and keep going forever.

              Yep, been in that kind of situation elsewhere. Painful :)

              1 Reply Last reply Reply Quote 0
              • gde23
                gde23 last edited by gde23

                @SGaist: how can the QTableView automatically shrink the text?
                Also with some fontmetrics stuff?

                @JonB Ok, so at least I'm not the only one who misses some good solution to this problem : )

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  Sorry, my answer was incomplete. You would need a custom QStyledItemDelegate that does the computation. What I wanted to point is that it might be easier to achieve with that setup.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply Reply Quote 2
                  • gde23
                    gde23 last edited by

                    @SGaist Thanks for the idea.
                    I'll give that a try, since with the column size not being updated by some layout this shouldn't have the problem with the oscillation.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post