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. Automatically shrink text in a table like structure to fit widget size
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 475 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.
  • gde23G Offline
    gde23G Offline
    gde23
    wrote on last edited by
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • gde23G gde23

      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?

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

      @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
      1
      • gde23G Offline
        gde23G Offline
        gde23
        wrote on last edited by
        #3

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

        JonBJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          1
          • gde23G gde23

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

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @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
            0
            • gde23G Offline
              gde23G Offline
              gde23
              wrote on last edited by gde23
              #6

              @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
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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
                2
                • gde23G Offline
                  gde23G Offline
                  gde23
                  wrote on last edited by
                  #8

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

                  • Login

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