You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Friday, December 30th 2011, 2:22pm

Convert QWebView to QImage

Hi
I would like to get the HTML page displayed with QWebView as a QImage. I have seen that it could be possible using QWidget::render, but I don't know how to obtain from QPainter a QPixmap or a QImage object.
thanks
Bogdan

2

Friday, December 30th 2011, 4:14pm

doesn't grabWidget work more easily?

Source code

1
2
QPixmap::grabWidget(your_widget_pointer).toImage();
//                 Qpixmap here             QImage here.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Saturday, December 31st 2011, 8:16am

It is exactly what I need.

happy new year !

4

Sunday, January 1st 2012, 7:22pm

I still have a problem because when the page is too large the scroll bar is displayed. I have tried the following code:

textView_ = new QWebView();
textView_->page()->setNetworkAccessManager(&model_);
connect(textView_, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinished(bool)));
textView_->load(QUrl::fromLocalFile(pageName));
eventloop_.exec();//wait for load to complete
textView_->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
QImage image = QPixmap::grabWidget(textView_).toImage();

but I still get the standard size of the widget with the vertical scroll bar displayed. How can I force QWebView to display the entire web page with no scroll bars (I do not want to hide the scroll bars, but to display completely the web page into the widget) ?

This post has been edited 1 times, last edit by "cristeab" (Jan 1st 2012, 7:29pm)


5

Sunday, January 1st 2012, 10:43pm

that is more complicated. I would look into off screen rendering.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

6

Saturday, January 7th 2012, 9:59pm

I have came up with the following solution:

QWebFrame *webFrame = webView_->page()->mainFrame();
webFrame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
webFrame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
webView_->setGeometry(QRect(QPoint(0, 0), webFrame->contentsSize()));
return QPixmap::grabWidget(webView_).toImage();

which does what it should, but this code should be run only from the GUI thread, otherwise I get warnings since QPixmap cannot be used in other threads. This raises another question: Is there a way to use QPixmap outside the GUI thread ? I need to do so since my application needs to handle different document formats among which CHM, so that some pages (in this case HTML files) are loaded in a worker thread.

7

Saturday, January 7th 2012, 11:05pm

pixmap shouldnt be used outside of gui thread. If you need work to be done in another thread, you need to start with pixmap in gui thread and convert to QImage and send that into the thread.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.