Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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"
MainWindow::MainWindow() : webview(new QWebView(this)) {
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;
}
.hide {
display: none;
}
</style>
</head>
<body>
<ul class="list">
<li>みょん</li>
</ul>
</body>
</html>
)");
auto addButton = new QPushButton("Add");
auto delButton = new QPushButton("Delete");
auto showButton = new QPushButton("Show");
auto hideButton = new QPushButton("Hide");
auto htmlButton = new QPushButton("Show HTML");
connect(addButton, SIGNAL(clicked()), this, SLOT(addItem()));
connect(delButton, SIGNAL(clicked()), this, SLOT(deleteItem()));
connect(showButton, SIGNAL(clicked()), this, SLOT(showList()));
connect(hideButton, SIGNAL(clicked()), this, SLOT(hideList()));
connect(htmlButton, SIGNAL(clicked()), this, SLOT(showHtml()));
auto buttonLayout = new QHBoxLayout();
buttonLayout->addWidget(addButton);
buttonLayout->addWidget(delButton);
buttonLayout->addWidget(showButton);
buttonLayout->addWidget(hideButton);
buttonLayout->addWidget(htmlButton);
auto layout = new QVBoxLayout();
layout->addWidget(webview);
layout->addLayout(buttonLayout);
auto mainwidget = new QWidget();
mainwidget->setLayout(layout);
this->setCentralWidget(mainwidget);
}
MainWindow::~MainWindow() {}
void MainWindow::addItem() {
auto list = webview->page()->mainFrame()->findFirstElement("ul.list");
list.appendInside("<li>" + QTime::currentTime().toString() + "</li>");
}
void MainWindow::deleteItem() {
auto item = webview->page()->mainFrame()->findFirstElement("ul.list li");
item.removeFromDocument();
}
void MainWindow::showList() {
auto list = webview->page()->mainFrame()->findFirstElement("ul.list");
list.removeClass("hide");
}
void MainWindow::hideList() {
auto list = webview->page()->mainFrame()->findFirstElement("ul.list");
list.addClass("hide");
}
void MainWindow::showHtml() {
qDebug() << webview->page()->mainFrame()->toHtml();
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtWebKitWidgets>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow();
~MainWindow();
private slots:
void addItem();
void deleteItem();
void showList();
void hideList();
void showHtml();
private:
QWebView* webview;
};
#endif
QT += core gui webkitwidgets
TEMPLATE = app
TARGET = QWebView_dom
INCLUDEPATH += .
HEADERS += mainwindow.h
SOURCES += main.cc mainwindow.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.