Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <QMessageBox>
#include "jsobj.h"
JsObj::JsObj(QObject *parent) : QObject(parent) {}
void JsObj::hogeSlot() {
emit hogeSignal();
}
#ifndef JSOBJ_H
#define JSOBJ_H
#include <QtCore>
class JsObj : public QObject {
Q_OBJECT
public:
JsObj(QObject *parent = nullptr);
signals:
void hogeSignal();
public slots:
void hogeSlot();
};
#endif
#include <QApplication>
#include "mainwindow.h"
auto main(int argc, char **argv) -> int {
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
#include <QtWebKitWidgets>
#include "mainwindow.h"
#include "jsobj.h"
MainWindow::MainWindow() {
auto jo = new JsObj();
auto button = new QPushButton("You also like");
connect(button, SIGNAL(clicked()), jo, SLOT(hogeSlot()));
auto webview = new QWebView(this);
webview->page()->mainFrame()->addToJavaScriptWindowObject("jsobj", jo);
webview->setHtml(R"(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
html {
font: 16px/1.6 Roboto, 'Droid Sans', 'Hiragino Kaku Gothic ProN', sans-serif;
}
</style>
<script>
var fugaSlot = function() {
location.href = 'http://myon.info/';
};
jsobj.hogeSignal.connect(fugaSlot);
</script>
</head>
<body>
<p>Please click the button below.</p>
</body>
</html>
)");
auto layout = new QVBoxLayout();
layout->addWidget(webview);
layout->addWidget(button);
auto mainwidget = new QWidget();
mainwidget->setLayout(layout);
this->setCentralWidget(mainwidget);
}
MainWindow::~MainWindow() {}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow();
~MainWindow();
};
#endif
QT += core gui webkitwidgets
TEMPLATE = app
TARGET = QWebView_cpp-to-html
INCLUDEPATH += .
HEADERS += mainwindow.h jsobj.h
SOURCES += main.cc mainwindow.cc jsobj.cc
CONFIG += c++11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.