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

[SOLVED] Getting size of viewable aread of QTextEdit



  • Hi all,

    I'd like to get the size of the viewable area of QTextWidget. What I mean by "viewable area" is the window in which the user can see the document content. This viewable area does not include the scrollbars (if they are shown), just the content windows.

    The layout of the application is such that I have a QTabWidget which contains the QTextEditor widget. So what I would expect to happen is that if I do the following sequence:

    1. Open a new tab
    2. Resize application window
    3. Open a new tab

    the tabs opened at step 1 and 3 would show a difference size. However, they don't. The sizes returned by QWidget.size() on both the editor widgets are the same.

    I don't understand how that could be. Any help?



  • Any ideas on how I can get the size I am looking for?


  • Lifetime Qt Champion

    @voidtrance

    Have a look at QTextDocument::size()
    You get from the document() function.

    This is just guessing. Did not code test it :)



  • @mrjj said:

    @voidtrance

    Have a look at QTextDocument::size()
    You get from the document() function.

    This is just guessing. Did not code test it :)

    I am not exactly sure what QTextDocument::size() returns but it is not the size of the visible area of the document. In fact, if I follow the same procedure as in the original post, the size printed return by QTextDocument::size() is exactly the same regardless of the fact that the visible area has been shrunk to about 25%.


  • Lifetime Qt Champion

    @voidtrance
    And you have set a layout so that the editor auto adjust to the size of the tab widget and the form
    also has a layout that makes the tabwidget auto adjust to the size of the form?

    I must be doing something wrong here as
    ui->textEdit->size().width();

    Does report different sizes when i resize the form.

    sorry for not understanding.



  • @mrjj said:

    @voidtrance
    And you have set a layout so that the editor auto adjust to the size of the tab widget and the form
    also has a layout that makes the tabwidget auto adjust to the size of the form?

    I must be doing something wrong here as
    ui->textEdit->size().width();

    Does report different sizes when i resize the form.

    sorry for not understanding.

    I am a little confused about your question regarding layouts but as far as I know everything is setup correctly.

    The QTextEdit widget in question is embedded in several layers of other widgets but they all have their layouts set correctly. The QTextEdit widget properly changes when I am resize the main window. Also, the scrollbars properly resize themselves to represent the chunk of visible content when I resize the main window.

    All of that leads me to believe that I have them embedded properly. However, QTextEdit->size() still returns the same size regardless. Therefore, I am thinking that I am doing something wrong. I'll investigate further.


  • Lifetime Qt Champion

    @voidtrance
    Ok. what Im asking is if you setup the layouts but from your answer,
    it seems you have them setup and working as
    expected.

    My mini test must be wrong in some regards as Size() before and after resize mainwindow does
    give changed values.

    If you make a mini sample using the forms editor and just placing a tabwidget
    and give it 2 tabs with TextEdits, does size() then give you expected values?



  • OK, well, I feel dumb!

    The reason why the size printed every time was the same is because I was printing the size prior to the QTextEdit widget receiving its initial resizeEvent(). Therefore, I kept getting some default size and not the actual size after Qt had properly placed the widget.

    Marking as SOLVED.


  • Lifetime Qt Champion

    @voidtrance
    Ahh. And since i did via a button, i was way past resizeEvent()

    Thank you for updating.


Log in to reply