I created an easy login form in html with a submit button that when I clicked it, a Javascript's function check if the login is correct or not.
All put all files into a qrc file in QT and I wrote this code for run the program:
login.pro
QT += webenginewidgets core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = login
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
RESOURCES += \
login.qrc
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtWebEngineWidgets>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void connectToJs(bool result, QWebEngineView *view);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWebEngineWidgets>
#include <QUrl>
#include <QtWebView/QtWebView>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFile apiFile(":/qtwebchannel/qwebchannel.js");
if(!apiFile.open(QIODevice::ReadOnly))
qDebug()<<"Couldn't load Qt's QWebChannel API!";
QString apiScript = QString::fromLatin1(apiFile.readAll());
apiFile.close();
view->page()->runJavaScript(apiScript);
view->load(QUrl(QStringLiteral("qrc:/new/prefix1/index.html")));
connect(view->page(), SIGNAL(loadFinished(bool)), this, SLOT(connectToJs(bool, view)));
setCentralWidget(view);
}
void MainWindow::connectToJs(bool result, QWebEngineView * view) {
qDebug() << "connectToJs!" << result;
if (result) {
QWebChannel *channel = new QWebChannel(view->page());
view->page()->setWebChannel(channel);
channel->registerObject(QString("nameObject"), this);
}
}
MainWindow::~MainWindow()
{
delete ui;
}
index.html
<html>
<head>
<title>Javascript Login Form Validation</title>
<script src="login.js"></script>
</head>
<body>
<div class="container">
<div class="main">
<h2>Javascript Login Form Validation</h2>
<form id="form_id" method="post" name="myform">
<label>User Name :</label>
<input type="text" name="username" id="username"/>
<label>Password :</label>
<input type="password" name="password" id="password"/>
<input type="button" value="Login" id="submit" onclick="validate()"/>
</form>
</div>
</div>
</body>
</html>
login.js
function validate(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if ( username == "Jack" && password == "login"){
alert ("Login successfully");
}
else{
alert("Error: the login is incorrect!");
}
}
var workoutObject;
if (typeof qt != 'undefined') {
alert("Qt is OK!!");
new QWebChannel(qt.webChannelTransport, function (channel) {
workoutObject = channel.objects.nameObject;
});
}
else {
alert("Qt is not defined!");
}
That I want now is pass the username and the password from html to QT when I click the button.
How can I implement the QWebChannel?