Skip to content

Instantly share code, notes, and snippets.

@bojieli
Created April 29, 2014 16:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bojieli/11405774 to your computer and use it in GitHub Desktop.
Save bojieli/11405774 to your computer and use it in GitHub Desktop.
A sample tiny browser based on Qt 5 + Webkit
/* Simple Browser Sample
* Put this file (browser.h) and browser.cpp, browser.pro in one folder.
* Use Qt Creator with Qt 5 to compile the project.
*
* Content of browser.cpp
*
#include "browser.h"
int main(int argc, char** argv) {
QApplication app(argc, argv);
MainWindow window;
window.loadUrl(QUrl("http://lib.ustc.edu.cn/"));
window.showFullScreen();
return app.exec();
}
* Content of browser.pro
*
QT += widgets webkitwidgets
SOURCES = browser.cpp
HEADERS += \
browser.h
*/
#ifndef BROWSER_H
#define BROWSER_H
#include <QtGui>
#include <QtWebKitWidgets>
#include <QBoxLayout>
#include <QPushButton>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(void) {
content = new QWebView;
toolbar = new QToolBar;
location = new QLineEdit;
homeButton = new QPushButton(QString("Home"));
topBarLayout = new QBoxLayout(QBoxLayout::LeftToRight);
layout = new QBoxLayout(QBoxLayout::TopToBottom);
window = new QWidget;
progressBar = new QProgressBar;
content->setHtml(QString("<h1>Loading...</h1>"));
toolbar->addAction(content->pageAction(QWebPage::Back));
toolbar->addAction(content->pageAction(QWebPage::Forward));
toolbar->addAction(content->pageAction(QWebPage::Reload));
toolbar->addAction(content->pageAction(QWebPage::Stop));
topBarLayout->addWidget(toolbar);
topBarLayout->addWidget(location);
topBarLayout->addWidget(homeButton);
layout->addLayout(topBarLayout);
layout->addWidget(content);
layout->addWidget(progressBar);
window->setLayout(layout);
setCentralWidget(window);
progressBar->setMinimum(0);
progressBar->setMaximum(100);
QObject::connect(content, SIGNAL(loadFinished(bool)), this, SLOT(onLoadFinish()));
QObject::connect(content, SIGNAL(loadFinished(bool)), progressBar, SLOT(reset()));
QObject::connect(content, SIGNAL(loadProgress(int)), this, SLOT(onLoadProgress()));
QObject::connect(content, SIGNAL(loadProgress(int)), progressBar, SLOT(setValue(int)));
QObject::connect(location, SIGNAL(returnPressed()), this, SLOT(gotoUrl()));
QObject::connect(homeButton, SIGNAL(clicked()), this, SLOT(goHome()));
}
void loadUrl(const QUrl url) {
homeUrl = url;
content->load(url);
content->setFocus();
}
protected slots:
void gotoUrl() {
QUrl url = QUrl::fromUserInput(location->text());
content->load(url);
content->setFocus();
}
void goHome() {
content->load(homeUrl);
content->setFocus();
}
void onLoadFinish() {
setWindowTitle(QString("%1").arg(content->title()));
}
void onLoadProgress() {
location->setText(content->url().toString());
}
private:
QWebView *content;
QLineEdit *location;
QPushButton *homeButton;
QBoxLayout *layout;
QToolBar *toolbar;
QBoxLayout *topBarLayout;
QProgressBar *progressBar;
QWidget *window;
QUrl homeUrl;
};
#endif // BROWSER_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment