2014年7月2日 星期三

[Qt4] How to change dynamically QWebFrame contents?

[Qt4] How to change dynamically QWebFrame contents?


Firstly, the setHtml will cause the loadFinished signal to be posted again, causing an infinite loop. I have attached working code of what you want to do.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWebKit/QWebView>
 
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(slotloadFinished(bool)));
    ui->webView->load(QUrl("http://www.google.com"));
}
 
MainWindow::~MainWindow()
{
    disconnect(ui->webView, 0, this, 0);
    delete ui;
}
 
void MainWindow::slotloadFinished(bool okay)
{
    disconnect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(slotloadFinished(bool)));
    ui->webView->setHtml("<html><body>test</body></html>");
    connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(slotloadFinished(bool)));
}
 
 
Instead of the disconnect and connect, you could do this..
 
void MainWindow::slotloadFinished(bool okay)
{
    ui->webView->blockSignals(true);
 
    ui->webView->setHtml("<html><body>test</body></html>");
 
    ui->webView->blockSignals(false);
} 


Ref:
http://qt-project.org/forums/viewthread/6742/#39382

2014年7月1日 星期二

[Qt4] How I can get all “input” elements from webview ?

[Qt4] How I can get all “input” elements from webview ?


void Class::getElements()
{
    QWebElementCollection collection = ui->cseriveOpenAPI_WebView->page()->mainFrame()->findAllElements("input");
    foreach (QWebElement element, collection)
    {
        qDebug() << "title:" << element.attribute("title");
    }
}
 
Ref :
https://qt-project.org/forums/viewthread/28920
http://qt-project.org/forums/viewthread/11604
http://stackoverflow.com/questions/2034981/how-to-manipulate-pages-content-inside-webkit-window-using-qt-and-qtwebkit