Skip to content

Instantly share code, notes, and snippets.

@Tosainu
Created January 18, 2015 07:52
Show Gist options
  • Save Tosainu/b6180de2eb852fc7eea8 to your computer and use it in GitHub Desktop.
Save Tosainu/b6180de2eb852fc7eea8 to your computer and use it in GitHub Desktop.
#include "jsobj.h"
JsObj::JsObj(QObject *parent) : QObject(parent) {}
void JsObj::setTextArea(QString s) {
emit textAreaChanged(s);
}
void JsObj::setQTextEdit(QString s) {
emit qTextEditChanged(s);
}
#ifndef JSOBJ_H
#define JSOBJ_H
#include <QtCore>
#include <QString>
class JsObj : public QObject {
Q_OBJECT
public:
JsObj(QObject *parent = nullptr);
signals:
void textAreaChanged(QString);
void qTextEditChanged(QString);
public slots:
void setTextArea(QString s);
void setQTextEdit(QString s);
};
#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 "mainwindow.h"
#include "jsobj.h"
MainWindow::MainWindow() {
textedit = new QTextEdit();
auto jo = new JsObj();
connect(this, SIGNAL(textChanged(QString)), jo, SLOT(setTextArea(QString)));
connect(jo, SIGNAL(qTextEditChanged(QString)), textedit, SLOT(setPlainText(QString)));
auto button = new QPushButton("Send to QWebView");
connect(button, SIGNAL(clicked()), this, SLOT(onClick()));
auto webview = new QWebView(this);
webview->setMaximumWidth(400);
webview->setMaximumHeight(400);
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;
}
textarea {
height: 200px;
width: 100%;
}
</style>
<script>
jsobj.textAreaChanged.connect(function(s) {
document.getElementById("test").value = s;
});
var onClick = function() {
jsobj.setQTextEdit(document.getElementById("test").value);
};
</script>
</head>
<body>
<p>QWebView</p>
<textarea id="test"></textarea>
<button onClick="onClick();">Send to QTextEdit</button>
</body>
</html>
)");
auto right = new QVBoxLayout();
right->addWidget(new QLabel("QWidget"));
right->addWidget(textedit);
right->addWidget(button);
auto layout = new QHBoxLayout();
layout->addWidget(webview);
layout->addLayout(right);
auto mainwidget = new QWidget();
mainwidget->setLayout(layout);
this->setCentralWidget(mainwidget);
}
MainWindow::~MainWindow() {}
void MainWindow::onClick() {
emit textChanged(textedit->toPlainText());
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWebKitWidgets>
#include <QMainWindow>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow();
~MainWindow();
signals:
void textChanged(QString s);
private slots:
void onClick();
private:
QTextEdit* textedit;
};
#endif
QT += core gui webkitwidgets
TEMPLATE = app
TARGET = QWebView_exchange-value
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