使用Qt实现一个简易的网页浏览器
实现功能如下:
- 在地址栏中输入地址按下回车跳转
- 网页后退功能
- 网页前进功能
- 网页刷新功能
核心代码:
界面设计
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
QHBoxLayout *buttonLayout = new QHBoxLayout();
backButton = new QPushButton("<-", this);
forwardButton = new QPushButton("->", this);
refreshButton = new QPushButton("Refresh", this);
goButton = new QPushButton("Go", this);
buttonLayout->addWidget(backButton);
buttonLayout->addWidget(forwardButton);
buttonLayout->addWidget(refreshButton);
urlLineEdit = new QLineEdit(this);
urlLineEdit->setPlaceholderText("Enter URL");
buttonLayout->addWidget(urlLineEdit);
buttonLayout->addWidget(goButton);
QMenuBar *menuBar = new QMenuBar(this);
menuBar->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); // 设置菜单栏大小策略
// 创建菜单
QMenu *fileMenu = menuBar->addMenu("File");
QMenu *editMenu = menuBar->addMenu("Edit");
QMenu *viewMenu = menuBar->addMenu("View");
QMenu *helpwMenu = menuBar->addMenu("Help");
// 创建菜单项
QAction *newAction = new QAction("New", this);
QAction *openAction = new QAction("Open", this);
QAction *saveAction = new QAction("Save", this);
QAction *cutAction = new QAction("Cut", this);
QAction *copyAction = new QAction("Copy", this);
QAction *pasteAction = new QAction("Paste", this);
QAction *zoomInAction = new QAction("Zoom In", this);
QAction *zoomOutAction = new QAction("Zoom Out", this);
// 将菜单项添加到菜单中
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
editMenu->addAction(cutAction);
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);
viewMenu->addAction(zoomInAction);
viewMenu->addAction(zoomOutAction);
webView = new QWebEngineViewRewrite(this);
layout->addWidget(menuBar);
layout->addLayout(buttonLayout);
layout->addWidget(webView);
信号和槽的连接
connect(backButton, &QPushButton::clicked, webView, &QWebEngineView::back);
connect(forwardButton, &QPushButton::clicked, webView, &QWebEngineView::forward);
connect(refreshButton, &QPushButton::clicked, webView, &QWebEngineView::reload);
connect(webView, &QWebEngineView::loadFinished, this, &BrowserWindow::updateButtons);
connect(goButton, &QPushButton::clicked, this, &BrowserWindow::handleButtonClicked);
BrowserWindow.h
private slots:
void updateButtons();
void loadUrl();
void handleButtonClicked();
void handleLinkHovered(const QUrl& url);
private:
QLineEdit *urlLineEdit;
QPushButton *backButton;
QPushButton *forwardButton;
QPushButton *goButton;
QPushButton *refreshButton;
QWebEngineView *webView;
QUrl m_url;
private:
Ui::BrowserWindow *ui;
BrowserWindow.cpp
void BrowserWindow::updateButtons()
{
backButton->setEnabled(webView->history()->canGoBack());
forwardButton->setEnabled(webView->history()->canGoForward());
}
void BrowserWindow::loadUrl()
{
QString url = urlLineEdit->text();
if (!url.isEmpty())
{
if (!url.startsWith("http://") && !url.startsWith("https://"))
{
url.prepend("http://");
}
QUrl webUrl(url);
webView->setUrl(webUrl);
connect(webView->page(), &QWebEnginePage::linkHovered, this, &BrowserWindow::handleLinkHovered);
}
}
void BrowserWindow::handleButtonClicked()
{
loadUrl();
updateButtons();
}
void BrowserWindow::handleLinkHovered(const QUrl &url)
{
//获取超链接
m_url = url;
qDebug() << "url:" << m_url;
}
关键的网页超链接跳转功能(核心)
需要重写事件和事件过滤函数
protected:
QWebEngineView* createWindow(QWebEnginePage::WebWindowType type) override;
private slots:
void LinkHovered(const QString& url);
private:
QUrl url_;
QVector<QObject* > mvchildObj;
protected:
bool event(QEvent* evt)override;
bool eventFilter(QObject* obj, QEvent* ev)override;
最终效果如下图:
如果有兴趣的话可以下载一下
https://download.csdn.net/download/liutao_1/88003602?spm=1001.2014.3001.5503