首先要写一个继承QWebEngineView的类
头文件:
#ifndef WEBBROWSER_H #define WEBBROWSER_H #include <QWebEngineView> #include <QWidget> namespace netsharp { class WebBrowser : public QWebEngineView { Q_OBJECT public: explicit WebBrowser(QWidget* parent = Q_NULLPTR); protected: /** * @brief createWindow 在鼠标左键点击的时候会触发这个方法 * @param type * @return */ QWebEngineView *createWindow(QWebEnginePage::WebWindowType type); private : }; } #endif // WEBBROWSER_H
实现文件:
#include "webbrowser.h" #include "MainWindow.h" #include <QWidget> #include<QMessageBox> #include<QMainWindow> namespace netsharp { WebBrowser::WebBrowser(QWidget *parent) : QWebEngineView(parent) { } QWebEngineView *WebBrowser::createWindow(QWebEnginePage::WebWindowType type) { WebBrowser *webbrowser = new WebBrowser(this); QMainWindow *dialog = new QMainWindow() ; dialog->setCentralWidget(webbrowser); dialog->show(); return webbrowser; } }
主窗口调用文件代码(mainwindow.cpp)
#include "mainwindow.h" #include "ui_mainwindow.h" #include "WebBrowser.h" #include "QSystemTrayIcon" #include "QMessageBox" #include <QTimer> #include <QUrl> using namespace netsharp; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); this->webbrowser = new WebBrowser(this); this->webbrowser->load(QUrl("http://beehive.gongsibao.com")); this->setCentralWidget(this->webbrowser); }
另:
QWebEngineView在QT Creator开发的时候网页加载特别慢,使用release方式即可解决,嗖嗖快