Created
January 31, 2015 03:08
-
-
Save Tosainu/1a678513e0496384de7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include "mainwindow.h" | |
auto main(int argc, char **argv) -> int { | |
QApplication app(argc, argv); | |
MainWindow window; | |
window.show(); | |
return app.exec(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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