查看完整版本: [-- QWebengine玩转HTML --]

QTCN开发网 -> Qt代码秀 -> QWebengine玩转HTML [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

305750665 2019-04-12 12:33

QWebengine玩转HTML









  1. QString readFiledata(const QString& filepath)
    {
        QFile file;
        file.setFileName(filepath);
        file.open(QIODevice::ReadOnly);
        QString data = file.readAll();
        file.close();
        return data;
    }

    void WebView::initControl()
    {
        qputenv("QTWEBENGINE_REMOTE_DEBUGGING", "9223");
        m_jQuery = readFiledata(":/WebenginePlay/Resources/jquery.min.js");
        m_jQuery.append("\nvar qt = { 'jQuery': jQuery.noConflict(true) };");

        connect(this, SIGNAL(titleChanged(QString)), SLOT(adjustTitle()));
        connect(this, SIGNAL(loadProgress(int)), SLOT(setProgress(int)));
        connect(this, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));
    }

    void WebView::loadUrl(QUrl url)
    {
        this->load(url);
    }

    void WebView::adjustTitle()
    {
        if (m_progress <= 0 || m_progress >= 100)
            parentWidget()->setWindowTitle(this->title());
        else
            parentWidget()->setWindowTitle(QString("%1 (%2%)").arg(this->title()).arg(m_progress));
    }

    void WebView::setProgress(int p)
    {
        m_progress = p;
        adjustTitle();
    }

    void WebView::finishLoading(bool)
    {
        m_progress = 100;
        adjustTitle();
        QString code = "qt.jQuery('<link>').attr({ rel: \"stylesheet\",type: \"text/css\",href: \"qrc:/WebenginePlay/Resources/rotate.css\"}).appendTo(\"head\"); undefined";
        m_jQuery.append("\n");
        m_jQuery.append(code);
        page()->runJavaScript(m_jQuery);
    }

    void WebView::highlightAllLinks()
    {
        QString code = "qt.jQuery('a').each( function () { qt.jQuery(this).css('background-color', 'yellow') } ); undefined";
        this->page()->runJavaScript(code);
    }

    void WebView::rotateImages()
    {
        QString code = "qt.jQuery('img').addClass(\"header\");";
        this->page()->runJavaScript(code);
    }

    void WebView::removeGifImages()
    {
        QString code = "qt.jQuery('[src*=gif]').remove()";
        this->page()->runJavaScript(code);
    }

    void WebView::updateBackground()
    {
        QString code = "qt.jQuery('body').each( function () { qt.jQuery(this).css('background', 'rgba(200,200,200,100)') } ); undefined";
        this->page()->runJavaScript(code);
    }





liulin188 2019-04-12 12:49

玖零儛 2019-04-12 14:45
支持jQuery哇,太强了

liudianwu 2019-04-17 13:05
玩得很溜!

朝东向西 2019-06-17 12:00
QWebengineView上层添加带圆角的QLabel控件,然后QLabel调用了winId(),此时圆角会变成黑色的,请问有人遇到过这个问题么?

zxjrainbow 2019-07-10 11:13
好内容。又学到了。


查看完整版本: [-- QWebengine玩转HTML --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled