Browser.cpp
Browser::Browser()
{
pageView = new QWebView(this);
pageView->load(QUrl("http://www.baidu.com/"));
setCentralWidget(pageView);
pageView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
pageLine = new QLineEdit(this);
pageLine->setText("http://www.baidu.com/");
createActions();
createToolBars();
createStatusBar();
connect(pageView, SIGNAL(loadProgress(int)), this, SLOT(refreshLine()));
connect(pageLine, SIGNAL(returnPressed()), this, SLOT(loadPage()));
connect(pageView, SIGNAL(linkClicked(const QUrl)), SLOT(loadLinkPage(const QUrl)));
}
void Browser::createActions()
{
backAct = new QAction(QIcon(":/images/back.png"), tr("&Back"), this);
backAct->setShortcut(tr("Ctrl+B"));
connect(backAct, SIGNAL(triggered()), this, SLOT(backPage()));
forwardAct = new QAction(QIcon(":/images/forward.png"), tr("&Forward"), this);
forwardAct->setShortcut(tr("Ctrl+F"));
connect(forwardAct, SIGNAL(triggered()), this, SLOT(forwardPage()));
reloadAct = new QAction(QIcon(":/images/reload.png"), tr("&Refresh"), this);
reloadAct->setShortcut(tr("Ctrl+R"));
connect(reloadAct, SIGNAL(triggered()), this, SLOT(reloadPage()));
}
void Browser::createToolBars()
{
navigationToolBar = addToolBar(tr("Navigation"));
navigationToolBar->addAction(backAct);
navigationToolBar->addAction(forwardAct);
navigationToolBar->addAction(reloadAct);
navigationToolBar->addWidget(pageLine);
}
void Browser::createStatusBar()
{
statusBar()->showMessage(tr("Ready"));
}
void Browser::loadPage()
{
pageView->load(QUrl(pageLine->text()));
}
void Browser::backPage()
{
pageView->back();
}
void Browser::forwardPage()
{
pageView->forward();
}
void Browser::reloadPage()
{
pageView->reload();
}
void Browser::refreshLine()
{
pageLine->setText(pageView->url().toString());
}
void Browser::loadLinkPage(const QUrl & url)
{
pageView->load(url);
}
Browser.h
#ifndef BROWSER_H #define BROWSER_H #include #include #include #include class Browser : public QMainWindow { Q_OBJECT public: Browser(); private slots: void loadLinkPage(const QUrl & url); void loadPage(); void backPage(); void forwardPage(); void reloadPage(); void refreshLine(); private: void createActions(); void createToolBars(); void createStatusBar(); QToolBar *navigationToolBar; QAction *backAct; QAction *forwardAct; QAction *reloadAct; QWebView *pageView; QLineEdit *pageLine; }; #endif