Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6537297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:36:00+00:00 2026-05-25T10:36:00+00:00

How do I react on the resize of a QMainWindow ? I have QTextBrowsers

  • 0

How do I react on the resize of a QMainWindow? I have QTextBrowsers in a QScrollArea and I adjust them to the size of the content on creating them (the only thing that should scroll is the QScrollArea).

Everything works for now, but if I resize the mainWindow, the height of the QTextBrowsers isn’t changed, because the reflow function isn’t triggered.

Do you have any better idea to adjust a QTextBrowser to it’s content? My current code is:

void RenderFrame::adjustTextBrowser(QTextBrowser* e) const {
    e->document()->setTextWidth(e->parentWidget()->width());
    e->setMinimumHeight(e->document()->size().toSize().height());
    e->setMaximumHeight(e->minimumHeight());
}

The parentWidget() is necessary because running width() on the widget itself returns always 100, regardless of the real size.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:36:01+00:00Added an answer on May 25, 2026 at 10:36 am

    If there is only text or html, you could use QLabel instead, because it already adapts its size to the available space. You’ll have to use:

    label->setWordWrap(true);        
    label->setTextInteractionFlags(Qt::TextBrowserInteraction); 
    

    to have almost the same behavior as a QTextBrowser.


    If you really want to use a QTextBrowser, you can try something like this (adapted from QLabel source code):

    class TextBrowser : public QTextBrowser {
        Q_OBJECT
    public:
        explicit TextBrowser(QWidget *parent) : QTextBrowser(parent) {
            // updateGeometry should be called whenever the size changes
            // and the size changes when the document changes        
            connect(this, SIGNAL(textChanged()), SLOT(onTextChanged()));
    
            QSizePolicy policy = sizePolicy();
            // Obvious enough ? 
            policy.setHeightForWidth(true);
            setSizePolicy(policy);
        }
    
        int heightForWidth(int width) const {
            int left, top, right, bottom;
            getContentsMargins(&left, &top, &right, &bottom);
            QSize margins(left + right, top + bottom);
    
            // As working on the real document seems to cause infinite recursion,
            // we create a clone to calculate the width
            QScopedPointer<QTextDocument> tempDoc(document()->clone());
            tempDoc->setTextWidth(width - margins.width());
    
            return qMax(tempDoc->size().toSize().height() + margins.height(),
                        minimumHeight());
        }
    private slots:
        void onTextChanged() {
            updateGeometry();
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface that needs to react to long key presses. That means
i have a rather weird problem that should fix a bug in my current
What I want to do is to react only on specified root elements. For
I writing a JSP program that needs to react on an existing program. It
I have a Rect object that I'd like to create and set its properties
I have a procedure to auto-resize a column in a grid to accommodate for
I have code which will draw a graph that scales if the user attempts
I have an 2D object (GL_QUAD) with size (W,H) and sitting at (-W*0.5, -H*0.5).
I have been trying for quite a while now to resize the desktop work
I have a form about the size of the screen height and am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.