PDA

View Full Version : How to adapt the size of QWebView to its contents



mycityofsky
28th May 2015, 15:39
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.sizeChang e)
self.setupFont()
self.sizeChange()

def setupFont(self):
self.htmlEditorSettings = self.settings()
self.htmlEditorSettings.setFontSize(QWebSettings.D efaultFontSize, self.fontSize)

def sizeChange(self):
docHeight = self.page().mainFrame().contentsSize().height()
self.setFixedHeight(docHeight)