Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to adapt the size of QWebView to its contents
Qt 6.11 is out! See what's new in the release blog

How to adapt the size of QWebView to its contents

Scheduled Pinned Locked Moved General and Desktop
pyqt5pyqtqwebview
1 Posts 1 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mycityofsky
    wrote on last edited by
    #1

    This is pyqt5 code, but it doesn't make much difference with its C++ version. You could also post C++ version here If you like. HtmlEditor should display only a line at first, with the height just enough to hold a line, not bigger, not smaller. As the user inputs more lines, the height of HtmlEditor will get bigger too. I tried to use self.page().mainFrame().contentsSize().height() as follows, it didn't work. I tried to replace self.page().mainFrame().contentsSize().height() with self.fontSize + self.contentsMargins().top() + self.contentsMargins().bottom(). But self.contentsMargins().top() and self.contentsMargins().bottom() are always zero. I don't know why. How to do the trick?

    class HtmlEditor(QWebView):
        def __init__(self, fontSize):
            super().__init__()
            self.fontSize = fontSize
            self.page().setContentEditable(True)
            self.page().contentsChanged.connect(self.sizeChange)
            self.setupFont()
            self.sizeChange()
            
        def setupFont(self):
            self.htmlEditorSettings = self.settings()
            self.htmlEditorSettings.setFontSize(QWebSettings.DefaultFontSize, self.fontSize)
        
        def sizeChange(self):
            docHeight = self.page().mainFrame().contentsSize().height()
            self.setFixedHeight(docHeight)
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved