QWebview and Qthread
-
Hi,
I am writing a qt browser application which is sending the screen shots of the webpage to an android tab through qt sockets.
@QString MainWindow::saveResult(){
QString file;if(view->page()->mainFrame()) { // save each frame in different image files file=saveFrame(view->page()->mainFrame()); } return file;
}
QString MainWindow::saveFrame(QWebFrame *frame)
{
static int frameCounter = 0;QString fileName="FRAMECAPTURE.jpg"; if (frameCounter) { int index = fileName.lastIndexOf('.'); fileName = fileName.insert(index, "_frame" + QString::number(frameCounter)); } QImage image(frame->contentsSize(), QImage::Format_ARGB32_Premultiplied); image.fill(Qt::transparent); QPainter painter(&image); painter.setRenderHint(QPainter::Antialiasing, true); painter.setRenderHint(QPainter::TextAntialiasing, true); painter.setRenderHint(QPainter::SmoothPixmapTransform, true); frame->render(&painter);
painter.end();
image.save("./CAPTUREIMAGES/"+fileName,"JPG");
++frameCounter;
foreach(QWebFrame *childFrame, frame->childFrames())
saveFrame(childFrame);
return fileName;}@
This is the code I used to save the webpage to a file and in next I am sending the image to a client ie, an application in android Tab.From the android Tab I am getting the coordinates of the images,buttons etc on the webpage and send back to the browser and I make changes to the webpage using the code below.
@
void MainWindow::processEvent(QPoint coordinate)
{QPoint pos=coordinate;
QMouseEvent event0(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(view->page(), &event0);
QMouseEvent event1(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QApplication::sendEvent(view->page(), &event1);QString fileName=saveResult();
qDebug()<<"FILENAME"<<file;
emit sendImage(fileName);
}
@
The problem comes when is , after posting the event to the view and calling saveResult(). The image saved is the the previous webpage and not the changed page after sendEvent().How can I save the changed webpage.Please help. -
Where and how is the QThread mentioned in the topic title involved?
-
actually the event is received over network and a thread started when client connection is established.thread actually recieves the input event and signals the processevent() function in mainwindow.
-
I think your problem is that you expect that the widget is completely updated after you have send the mouse events. It is not.
Mouse events may trigger changes in the web view. In such cases, the web view will call update(). This will schedule an update of the widget. The actual update happens on a next iteration of the event loop. However, at the moment. you've already rendered the view into your file and send that. You've circumvented using the event loop for the mouse events, but you can't do the same for the update cycle I think.
-
But the webpage is changing instantly receiving the mouse click events.
-
And as I explained, that does not mean that the rest of the whole update process is executed immediately. I only pointed at the paint update being asynchronous, but the web view itself may also do part of its work asynchronous. There simply is no guarantee that the view will be updated immediately after it receives clicks. Only that it will update if needed, but it is not telling you when.
-
So how can I make my saveResult() call only after the complete qwebview update. Is there any signals emitted or something like that,so i can call the saveResult() function to capture the changed webpage.?