Skip to content

Instantly share code, notes, and snippets.

@Tosainu
Last active August 19, 2016 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tosainu/bd4dfae31c0af3c8a8ef to your computer and use it in GitHub Desktop.
Save Tosainu/bd4dfae31c0af3c8a8ef to your computer and use it in GitHub Desktop.
#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