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

沒有留言:

張貼留言