zoukankan      html  css  js  c++  java
  • qt调用js,js调用qt

    复制代码
    <html>
    
    <script language="JavaScript">
    
        function qtcalljs()
        {
    
            alert("sdfsd");
        }
    
    
        function jscallqt()
        {
            mainWindow.dosome();
        }
    
    </script>
    
    
    <body>
    
    <button onclick="jscallqt()" style=="height:20px;100px"/>
    
    
    </body>
    </html>
    复制代码

    qt.h:

    复制代码
    #ifndef QTTT_H
    #define QTTT_H
    
    #include <QtGui/QMainWindow>
    #include <QWebKitPlatformPlugin>
    #include <QWebFrame>
    #include <QMessageBox>
    #include <QObject>
    #include "ui_qttt.h"
    
    class qttt : public QMainWindow
    {
        Q_OBJECT
    
    public:
        qttt(QWidget *parent = 0, Qt::WFlags flags = 0);
        ~qttt();
    
    private:
        Ui::qtttClass ui;
    
    private slots:
        void pop();
        void addMyObjectToJavascript();
    public:
        Q_INVOKABLE void dosome();
    };
    
    #endif // QTTT_H
    复制代码

    qt.cpp

    复制代码
    #include "qttt.h"
    
    qttt::qttt(QWidget *parent, Qt::WFlags flags)
        : QMainWindow(parent, flags)
    {
        ui.setupUi(this);
    
        ui.webView->load(QUrl("test.html"));
        connect(ui.pushButton,SIGNAL(clicked()),this,SLOT(pop()));
        connect(ui.webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(addMyObjectToJavascript()));
    
    
    }
    
    qttt::~qttt()
    {
    
    }
    
    void qttt::pop()
    {
    
        ui.webView->page()->mainFrame()->evaluateJavaScript("qtcalljs();");
    }
    
    void qttt::addMyObjectToJavascript()
    {
        ui.webView->page()->mainFrame()->addToJavaScriptWindowObject("mainWindow", this);
    }
    
    void qttt::dosome()
    {
        QMessageBox::aboutQt(this,"sdfsa");
    }
    复制代码
  • 相关阅读:
    布局
    JS基础回顾_滚动条
    JS基础回顾_Dom
    JS语法_其他
    JS语法_类型
    一些免费的API
    CSS特效(一)
    博客园在Markdown中使用JS
    C# 聊一聊屏保的设置 第一步 注册表
    2019 力扣杯-全国高校春季编程大赛 最长重复子串
  • 原文地址:https://www.cnblogs.com/tiandsp/p/7580588.html
Copyright © 2011-2022 走看看