Search code examples
c++qtqmlwebengine

Qt/QML: WebEngineView adjusting height


I have a bunch of WebEngineViews in a ListView. For example:

ListView 
{
    model: myModel
    delegate: Component 
    {
        Item 
        {
            WebEngineView 
            {
                id: myWebView
                Component.onCompleted: loadHtml(model.modelData.htmlText, baseURL)
            }
        }
    }
}

How can I adjust the height of the Item once the html is loaded? The HTML in question may include images, so I want to resize the WebEngineView once everything is loaded. I suspect I can do something like:

ListView 
{
    model: myModel
    delegate: Component 
    {
        Item 
        {
            WebEngineView 
            {
                id: myWebView
                Component.onCompleted: loadHtml(model.modelData.htmlText, baseURL)
                onLoadingChanged: 
                { 
                    if (!loading && loadRequest.status == WebEngineView.LoadSucceededStatus)
                    {
                        height = ???? 
                    }
                }
            }
        }
    }
}

But I'm not sure to what I should set height.


Solution

  • I was able to do what I wanted with `onContentsSizeChaged' like so:

    ListView 
    {
        model: myModel
        delegate: Component 
        {
            Item 
            {
                WebEngineView 
                {
                    id: myWebView
                    Component.onCompleted: loadHtml(model.modelData.htmlText, baseURL)
    
                    onContentsSizeChanged:
                    {
                        myWebView.height = contentsSize.height;
                    }
                }
            }
        }
    }