Qt5 QWebEngineView 一些心得

一、QWebEngineView网页跳转

 

webwindow.h

class WebWindow:public QWebEngineView

{

    Q_OBJECT

public:

    explicit WebWindow(QWidget*parent=nullptr);

protected:

    QWebEngineView*createWindow(QWebEnginePage::WebWindowType);//左键打开网页

};

 

webwindow.cpp

WebWindow::WebWindow(QWidget*parent):QWebEngineView(parent){}

QWebEngineView*WebWindow::createWindow(QWebEnginePage::WebWindowType)

{

    return this;

}//给方法为enumQWebEnginePage::WebWindowType

 

mainwindow.cpp//没有UI文件的情况下,而且在mainwindow.h已经声明webbrowser

MainWindow::MainWindow(QWidget *parent)

    :QMainWindow(parent)

{

    webbrowser=new WebWindow(this);

    webbrowser->load(QUrl("https://www.so.com/"));

}

 

MainWindow::~MainWindow(){}

 

二、QWebEngineView基本浏览功能

 

webwindow.h

加入

public:

    QUrl now_url;

public slots:

    void linkHovered(const QString &url);

 

webwindow.cpp

往WebWindow::WebWindow(QWidget *parent) :QWebEngineView(parent)中添加

connect(this->page(),&QWebEnginePage::linkHovered,this,&WebWindow::linkHovered);

修改createWindow(QWebEnginePage::WebWindowType)函数的内容

QWebEngineView*WebWindow::createWindow(QWebEnginePage::WebWindowType)

{

    this->load(now_url);

    return 0;

}

void WebWindow::linkHovered(const QString &url)

{

    now_url=url;

}

 

mainwindow.cpp

添加按钮,点击按钮实现以下功能

前进:webbrowser->forward();

后退:webbrowser->back();

刷新:webbrowser->reload();


三、QWebEngineView下载功能


webwindow.h

在其中添加

signals:

    void downloadRequested(QWebEngineDownloadItem *softdownload);

    void dataChanged(qint64,qint64);

 

public slots:

    void downloadJump(QWebEngineDownloadItem *softdownload);

    void setCurrentProgress(qint64 bytesreceived,qint64 bytestotal);


webwindow.cpp

往WebWindow::WebWindow(QWidget *parent) :QWebEngineView(parent)中添加

connect(this->page()->profile(),&QWebEngineProfile::downloadRequested,this,&WebWindow::downloadJump);

在cpp中添加

void WebWindow::downloadJump(QWebEngineDownloadItem*softdownload)

{

    softdownload->accept();

  connect(softdownload,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(setCurrentProgress(qint64,qint64)));

}

 

void WebWindow::setCurrentProgress(qint64 bytesreceived,qint64 bytestotal)

{

    emit dataChanged(bytesreceived,bytestotal);

}


mainwindow.cpp//已经声明进度条控件为progressBar

在MainWindow::MainWindow(QWidget *parent): QMainWindow(parent)中新建进度条控件为progressBar,再其中添加

connect(webbrowser,SIGNAL(dataChanged(qint64,qint64)),this,SLOT(append(qint64,qint64)));//WebEngineView传递参数给MainWindow

在cpp添加

void MainWindow::append(qint64 bytesreceived,qint64 bytestotal)

{

    progressBar->setValue(bytesreceived);

    progressBar->setMaximum(bytestotal);

    if(bytesreceived==bytestotal){

       progressBar->hide();

 qDebug()<<QString("下载完成");

    }

    if(bytesreceived!=bytestotal){

       progressBar->show();

qDebug()<<QString("正在下载");

    }

}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值