Qt开发-使用QWebEngineView开发一个简易的浏览器(一)

        简单的一个程序,在 之前文章的WebEngineView上增加了几个小功能


        环境:Qt5.9.1 QtCreator Win7 环境配置:http://blog.csdn.net/m32692529/article/details/78788337


        添加的功能:

        1、显示标题

        2、加载进度

        3、显示网页对应的图标


        首先上图,没有做美化,界面美化跟功能后续根据需要慢慢完善。


Qt开发-使用QWebEngineView开发一个简易的浏览器(一)_第1张图片


        首先绑定QWebEngineView的几个需要的信号,在当前类实现槽函数。

        Widget.h

public slots:
    void slt_loadFinished(bool success);//加载完成
    void slt_titleChange(const QString & title);//标题改变
    void slt_iconChanged(const QPixmap & icon);//图标改变
    void slt_loadProgress(int);//加载进度

        Widget.cpp

    connect(m_pWebEngineView,SIGNAL(loadProgress(int)),this,SLOT(slt_loadProgress(int)));
    connect(m_pWebEngineView,SIGNAL(loadFinished(bool)),this,SLOT(slt_loadFinished(bool)));
    connect(m_pWebEngineView,SIGNAL(titleChanged(QString)),this,SLOT(slt_titleChange(QString)));
    connect(m_pWebEngineView,SIGNAL(sig_iconChanged(QPixmap)),this,SLOT(slt_iconChanged(QPixmap)));

        槽函数实现

void Widget::slt_loadFinished(bool success){
    if (success && 100 != m_iProgress) {
        qDebug() << "slt_loadFinished progress is :" << m_iProgress;
    }
}

void Widget::slt_loadProgress(int progress){
    ui->progressBar->setValue(progress);

    if(progress == 100){
        ui->progressBar->hide();
    }
}

void Widget::slt_titleChange(const QString & title){
    ui->labelTitle->setText(title);
}

void Widget::slt_iconChanged(const QPixmap & pixmap){
    ui->labelIcon->setPixmap(pixmap);
}


        图标下载

MyWebEngineView::MyWebEngineView(QWidget * parent)
    : QWebEngineView(parent)
{
    m_pNetworkAccessManager = new QNetworkAccessManager();
    connect(this,SIGNAL(iconUrlChanged(QUrl)),this,SLOT(slt_iconUrlChanged(QUrl)));

}

void MyWebEngineView::loadUrl(const QUrl & url){
    this->setUrl(url);
}

void MyWebEngineView::slt_iconUrlChanged(const QUrl & url){
    QNetworkRequest iconRequest(url);
    m_iconReply = m_pNetworkAccessManager->get(iconRequest);
    connect(m_iconReply, SIGNAL(finished()), this, SLOT(slt_iconloaded()));
}

void MyWebEngineView::slt_iconloaded(){
    if (m_iconReply) {
        QByteArray data = m_iconReply->readAll();
        QPixmap pixmap;
        pixmap.loadFromData(data);
        pixmap = pixmap.scaled(20,20,Qt::KeepAspectRatio , Qt::SmoothTransformation);
        emit sig_iconChanged(pixmap);

        m_iconReply->deleteLater();
        m_iconReply = 0;
    }
}


你可能感兴趣的:(Qt开发-使用QWebEngineView开发一个简易的浏览器(一))