1.UI文件需要的控件
- 布局无所谓,有这些控件就可以
- 名字可以参考(对应代码)
2.代码
QWebEngineView.h
#include <QtWidgets/QMainWindow>
#include "ui_BrowserQWebEngineView.h"
namespace WebUse
{
class BrowserQWebEngineView : public QMainWindow
{
Q_OBJECT
public:
BrowserQWebEngineView(QWidget* parent = nullptr);
~BrowserQWebEngineView();
private slots:
void GoClicked();
void ResetClicked();
private:
Ui::BrowserQWebEngineViewClass ui;
};
}
QWebEngineView.cpp
#include "BrowserQWebEngineView.h"
namespace WebUse
{
BrowserQWebEngineView::BrowserQWebEngineView(QWidget* parent)
: QMainWindow(parent)
{
ui.setupUi(this);
ui.lineEdit->setText("https://");
connect(ui.Go, SIGNAL(clicked()), this, SLOT(GoClicked()));
connect(ui.Reset, SIGNAL(clicked()), this, SLOT(ResetClicked()));
}
BrowserQWebEngineView::~BrowserQWebEngineView()
{}
void BrowserQWebEngineView::GoClicked()
{
QString url = ui.lineEdit->text();
if (url != "")
{
ui.webEngineView->load(url);
}
}
void BrowserQWebEngineView::ResetClicked()
{
ui.webEngineView->reload();
}
}
3.效果
4.后记
核心就是调用QWebEngineView的两个API
- 调用webEngineView->load(url);
- 调用webEngineView->reload();