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?
Qt Code:
  1. class HtmlEditor(QWebView):
  2. def __init__(self, fontSize):
  3. super().__init__()
  4. self.fontSize = fontSize
  5. self.page().setContentEditable(True)
  6. self.page().contentsChanged.connect(self.sizeChange)
  7. self.setupFont()
  8. self.sizeChange()
  9.  
  10. def setupFont(self):
  11. self.htmlEditorSettings = self.settings()
  12. self.htmlEditorSettings.setFontSize(QWebSettings.DefaultFontSize, self.fontSize)
  13.  
  14. def sizeChange(self):
  15. docHeight = self.page().mainFrame().contentsSize().height()
  16. self.setFixedHeight(docHeight)
To copy to clipboard, switch view to plain text mode