Fitting the page content to the QWebView widget
-
Hi all,
I load web pages using QWebView widget, it's ok. But I wanna fit the page content to the QWebView widget (like zooming to fit) and no scrollbar should be displayed.
Could you tell me solution? I tried using webView->page()->setPreferredContentsSize() but it seems not to be a proper way.
Thanks!
-
I'm using Css Style Sheet files inside the Html files for the layout. Works fine.
-
[quote author="KA51O" date="1319797238"]I'm using Css Style Sheet files inside the Html files for the layout. Works fine.[/quote]
How about it? Could u post a piece of code? And what if we have to load html files outside?
Thanks and regards! -
You can set the StyleSheet for the QWebPage of your WebView like this:
@void MyApp::setStyleSheetFilePath(QString a_styleSheetUrl)
{
QWebElementCollection styleSheetLink = m_webView->page()->mainFrame()->findAllElements("link[rel=stylesheet]");
for(int i = 0; i < styleSheetLink.count(); ++i)
{
styleSheetLink.at(i).setAttribute("href", a_styleSheetUrl);
}
}@And CSS is pretty simple for a start "this":http://www.w3schools.com/cssref/default.asp might help you.
@
*{
margin: 0;
padding: 0;
}html
{
height: 100%;
width: 100%;
}body
{
border: 0;
height: 100%;
width: 100%;
font-family: "Times New Roman";
}@ -
But if the pages content is to big to fit into your WebView widget you will have a scrollbar. Maybe you can try to have different StyleSheet files for different widget sizes. Then you could define for example the font size accordingly and try to shrink the content this way. Also I don't think this will work as a general solution for content loaded from outside sources as different StyleSheets will affect the layout of these pages.
Have you tried "setZoomFactor(qreal factor)":http://developer.qt.nokia.com/doc/qt-4.7/qwebview.html#id-b17c838f-b378-4b3d-9eef-fd6435789f6a or "setViewportSize":http://developer.qt.nokia.com/doc/qt-4.7/qwebpage.html#id-cd982469-654f-4acb-bee1-af672bba8f69. A combination of "contentSize":http://developer.qt.nokia.com/doc/qt-4.7/qwebframe.html#id-b35b7b37-f485-40c2-9c6e-e35369c8f043 and the size of the WebView widget to calculate the zoomFactor might do the trick. But I'm just guessing.