Dear fxonair,
Sorry for my late post. This is a simple example which pops up an input dialog to enter text whenever you click on an html input field. Whishing it will be helpful!
mainwindow.h:
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "webbridge.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_webview_loadFinished(bool issuccessful);
private:
Ui::MainWindow *ui;
WebBridge m_webbridge;
};
#endif // MAINWINDOW_H@
mainwindow.cpp:
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWebKit>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->webview->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
ui->webview->settings()->setAttribute(QWebSettings::AutoLoadImages, true);
ui->webview->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
ui->webview->page()->mainFrame()->addToJavaScriptWindowObject("webbridge", &m_webbridge);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_webview_loadFinished(bool issuccessful)
{
if (issuccessful) {
const QString script = "var inputs = document.getElementsByTagName('INPUT');var index;for(index=0; index < inputs.length; index++){inputs[index].onclick = function() {this.value=webbridge.openInputContext(this.value);}}";
ui->webview->page()->mainFrame()->evaluateJavaScript(script);
}
}@
webbridge.h:
@#ifndef WEBBRIDGE_H
#define WEBBRIDGE_H
#include <QtCore>
class WebBridge : public QObject
{
Q_OBJECT
public:
WebBridge();
public slots:
QString openInputContext(const QString& orgtext);
};
#endif // WEBBRIDGE_H@
webbridge.cpp:
@#include "webbridge.h"
#include <QInputDialog>
WebBridge::WebBridge() :
QObject()
{
}
QString WebBridge::openInputContext(const QString &orgtext)
{
QString text = QInputDialog::getText(NULL, "Input Box", "Enter your text");
return (!text.isNull()) ? text : orgtext;
}@
Regards,