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

How to know QTextEdit'height, when widget is hidden?



  • Hi,

    I have QWidget. I have to set on it many QTextEdits with textes, which I don't know ( they are from database ). I setFixedWidth in this QTextEdits to 200. I don't know their height. If text will be short, QTextEdits' height will be small etc. I need to know their height.

    There is very good method for me from QTextDocument. I do:

    document()->size();
    

    but this function works only when QTextEdits are visible! It works when QTextEdits are hidden but get me height = 0.
    I want other method to this the same, but when QTextEdits are hidden and get me real height ( not 0 ).



  • @qwe3
    I don't know if this is right, but I would suspect you/Qt won't know QTextEdit height until it is shown, not if it's hidden....?



  • @JonB Ok. But maybe there is a way to calculate this before "show()"?



  • @qwe3 if height to be adjusted automatically when it's visible, then set the vertical size policy: Expanding and set the line wrap mode
    https://doc.qt.io/qt-5/qtextedit.html#lineWrapMode-prop
    Can you explain the usecase of your requirement. Is there any reason to get the size when it's hidden?



  • @nagesh said in How to know QTextEdit'height, when widget is hidden?:

    set the vertical size policy: Expanding and set the line wrap mode

    How will that give the OP the total height of the QTextEdits?



  • @JonB I hope even that will not give valid height when it's hidden, I was just trying to tell in the way like if Layout & size policy is set height will be set automatically when it's visible. (post edited)



  • @qwe3 said in How to know QTextEdit'height, when widget is hidden?:

    But maybe there is a way to calculate this before "show()"?

    I know that problem very well. So far, we have not found a solution to this than to show the widget first. Somehow the sizes of widgets inside a layout are not calculated before the widget is shown for the first time. There are two functions that are supposed to recalculate the layout (QLayout::update() and QWidget::adjustSize()), but they don't do anything in this context.


Log in to reply