qt使用webengine 条件
qt在windows上使用webengine必须用visual studio,使用mingw无效,webengine可以集成我们得html5页面,这样可以让界面开发人员更加省心。
code
1 包含qwebengineview
#include <QWidget>
#include <QWebEngineView>
#include <QMouseEvent>
头文件从QWebEngineView继承
#ifndef SWEBENGINEVIEW_H
#define SWEBENGINEVIEW_H
#include <QWidget>
#include <QWebEngineView>
#include <QMouseEvent>
class SWebEngineView : public QWebEngineView
{
Q_OBJECT
public:
explicit SWebEngineView(QWidget *parent = 0);
QList<QUrl> listUrl() const;
void addItemUrl(QUrl url);
void test(int i );
protected:
QWebEngineView *createWindow(QWebEnginePage::WebWindowType type);
private slots:
void slot_link(const QString &url)
{
v_url.setUrl(url);
}
private:
QWidget *v_parent = NULL;
QUrl v_url;
};
#endif
#include "swebengineview.h"
#include <iostream>
SWebEngineView::SWebEngineView(QWidget *parent) : QWebEngineView(parent)
{
setAttribute(Qt::WA_DeleteOnClose);
v_parent = parent;
connect(this->page(),&QWebEnginePage::linkHovered,this,&SWebEngineView::slot_link);
}
void SWebEngineView::test(int i)
{
}
QWebEngineView *SWebEngineView::createWindow(QWebEnginePage::WebWindowType type)
{
SWebEngineView *page = new SWebEngineView(this);
page->resize(this->size());
page->show();
return page;
}
其中createWindow比较重要
mainwindow调用
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
QRect deskRect = QApplication::desktop()->availableGeometry();
this->move(deskRect.left(), 0);
ui->setupUi(this);
m_view = new SWebEngineView(ui->verticalLayoutWidget);
m_view->setUrl(QUrl(QStringLiteral("https://www.csdn.net")));
m_view->show();
m_backBtn = new QPushButton(this);
m_backBtn->resize(100,30);
m_backBtn->move(0,0);
m_backBtn->setText(tr("Back"));
connect(m_backBtn, SIGNAL(clicked(bool)), this, SLOT(onBackUrl()));
}
添加事件响应函数
void MainWindow::resizeEvent(QResizeEvent *)
{
ui->verticalLayoutWidget->resize(this->size());
m_view->resize(this->size());
}
void MainWindow::onBackUrl()
{
m_view->back();
}
void MainWindow::on_pushButton_clicked()
{
QString url = ui->lineEdit->text();
if(!url.isEmpty())
{
m_view->load(url);
}
}
加载csdn页面测试 点击以后跳转 按back按钮可以返回
|