Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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



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



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


  • Lifetime Qt Champion

    Hi,

    Why not use a QTableView for that ?



  • @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 :)



  • @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 : )


  • Lifetime Qt Champion

    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.



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


Log in to reply