如何在QT中自定义“ Notification Web API”

吉列尔梅·纳西门托

我创建使用一个简单的浏览器QtWebKit的,我设法增加对支持通知的Web API它,使用QWebPage::setFeaturePermission

例子:

function notifyMe() {
    if (Notification.permission === "granted") {
        var notification = new Notification("Hi there!");
    } else if (Notification.permission !== "denied") {
        Notification.requestPermission(function(permission) {
            if (permission === "granted") {
                var notification = new Notification("Hi there!");
            }
        });
    }
}

<button onclick="notifyMe();">Notify me</button>

我的代码:

QObject::connect(page,
    SIGNAL(featurePermissionRequested(QWebFrame*, QWebPage::Feature)), this,
    SLOT(featurePermissionRequested(QWebFrame*,QWebPage::Feature))
);

...

void Form::featurePermissionRequested(QWebFrame* frame, QWebPage::Feature feature) {
    switch (feature) {
        case QWebPage::Notifications:
            qDebug() << "Notification";
            page->setFeaturePermission(frame, feature, QWebPage::PermissionGrantedByUser);
        break;
        case QWebPage::Geolocation:
            qDebug() << "GEO";
        break;
        default:
            qDebug() << "Unknown feature";
    }
}

每次我单击“通知我”按钮时,桌面上都会出现以下消息:

桌面通知

是否可以在QT中自定义通知?换句话说,保留类似于GoogleChrome或Firefox的方式,如下所示:

网络通知

吉列尔梅·纳西门托

要进行自定义Notifications Web APIQtWebkit您必须使用“ Webkit插件”,换句话说,创建一个插件并放入中qtdir/plugins/webkit

注意:对于需要创建插件的<QtWebKit/QWebKitPlatformPlugin>

创建插件:

  • 在QtCreator中创建一个项目
  • .pro文件中使用(示例src.pro):

    TARGET = $$qtLibraryTarget(mywebkitplugin)
    TEMPLATE = lib
    CONFIG += plugin
    
    HEADERS += $$[QT_INSTALL_HEADERS]/QtWebKit/qwebkitplatformplugin.h \
        mywebkitplugin.h
    
    SOURCES += \
        mywebkitplugin.cpp
    
    Release:DESTDIR     = $$PWD/bin/release
    Release:UI_DIR      = $${DESTDIR}/.ui
    Release:MOC_DIR     = $${DESTDIR}/.moc
    Release:RCC_DIR     = $${DESTDIR}/.rcc
    Release:OBJECTS_DIR = $${DESTDIR}/.obj
    
    Debug:DESTDIR       = $$PWD/bin/debug
    Debug:UI_DIR        = $${DESTDIR}/.ui
    Debug:MOC_DIR       = $${DESTDIR}/.moc
    Debug:RCC_DIR       = $${DESTDIR}/.rcc
    Debug:OBJECTS_DIR   = $${DESTDIR}/.obj
    
  • 创建mywebkitplugin.h

    #ifndef MYWEBKITPLUGIN_H
    #define MYWEBKITPLUGIN_H
    
    #include <QtWebKit/QWebKitPlatformPlugin>
    
    class MyWebKitPlugin : public QObject, public QWebKitPlatformPlugin
    {
        Q_OBJECT
        Q_INTERFACES(QWebKitPlatformPlugin)
    
    #if QT_VERSION >= 0x050000
        Q_PLUGIN_METADATA(IID "org.qtwebkit.QtWebKit.QtWebPlugin")
    #endif
    
    public:
        explicit MyWebKitPlugin();
        ~MyWebKitPlugin();
    
        bool supportsExtension(Extension ext) const;
        QObject* createExtension(Extension ext) const;
    };
    
    #endif // MYWEBKITPLUGIN_H
    
  • 创建mywebkitplugin.cpp

    #include "mywebkitplugin.h"
    #include "notification/notification.h"
    
    MyWebKitPlugin::MyWebKitPlugin()
    {
    }
    
    MyWebKitPlugin::~MyWebKitPlugin()
    {
    }
    
    bool MyWebKitPlugin::supportsExtension(Extension ext) const
    {
        return ext == Notifications;
    }
    
    QObject* MyWebKitPlugin::createExtension(Extension ext) const
    {
        switch (ext) {
            case Notifications:
                return new Notification();
    
            default:
                return 0;
        }
    }
    
    //for QT-4.8
    #if QT_VERSION < 0x050000
    Q_EXPORT_PLUGIN2(webkitplugin, MyWebKitPlugin);
    #endif
    
  • 建立资料notification

  • 在通知文件夹中放入通知类:

    notification.h

    #ifndef NOTIFICATION_H
    #define NOTIFICATION_H
    
    #include <QtWebKit/QWebKitPlatformPlugin>
    
    class Notification : public QWebNotificationPresenter
    {
        Q_OBJECT
    
    public:
        explicit Notification();
        ~Notification();
    
        void showNotification(const QWebNotificationData* data);
    
    signals:
        void notificationClosed();
        void notificationClicked();
    };
    
    #endif // NOTIFICATION_H
    

    notification.cpp

    #include "notification.h"
    #include <QDebug>
    
    Notification::Notification() : QWebNotificationPresenter()
    {
        qDebug() << "Create: Notification";
    }
    
    Notification::~Notification()
    {
        qDebug() << "Delete: this (Notification)";
    }
    
    void Notification::showNotification(const QWebNotificationData* data)
    {
        qDebug() << "title:" << data->title();
        qDebug() << "icon:" << data->iconUrl();
        qDebug() << "message:" << data->message();
        qDebug() << "opener page:" << data->openerPageUrl();
    }
    

用于创建通知的自定义更改Notification::showNotification(const QWebNotificationData* data)内容,并QWebNotificationData* data用于从获取数据JavaScript API

  • 创建notification.pri(包含在中src.pro):

    QT += network
    
    HEADERS += \
        $$PWD/notification.h
    
    SOURCES += \
        $$PWD/notification.cpp
    
  • 添加notification.prisrc.pro

    include($$PWD/notification/notification.pri)
    

编译/构建:

  • src.pro在QtCreator中打开
  • 单击Build(在释放模式)(或使用Ctrl+ B)按钮(不要点击Run按钮,不使用 Ctrl+ R
  • 关闭 src.pro
  • 转到位于 src.pro
  • (如果为释放模式)打开bin/release文件夹
  • (如果为调试模式)打开bin/debug文件夹
  • (如果释放模式)拷贝mywebkitplugin.dllQtDir/plugins/webkit/mywebkitplugin.dll(例如用MinGW的:C:/qt/qt5.4/mingw/plugin/webkit/mywebkitplugin.dll
  • (如果调试模式)拷贝mywebkitplugind.dllQtDir/plugins/webkit/mywebkitplugind.dll(例如用MinGW的:C:/qt/qt5.4/mingw/plugin/webkit/mywebkitplugind.dll
  • 如果webkit文件夹不存在,请创建它。
  • 使用打开您的项目QWebView并进行测试Notification Web API

运行使用的项目时,QWebView它将自动加载dll(在项目中不需要额外的配置),并将“自定义小部件” “替换”默认值NotificationsQtWebkit在Windows中SystemTrayIcon用于show Notification Web API)。

插件项目的文件夹结构:

mywebkitplugin
├── `src.pro`
├── mywebkitplugin.h
├── mywebkitplugin.cpp
└── notification
    ├── notification.h
    ├── notification.cpp
    └── `notification.pri`

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在ASP.NET Framework Web API中返回自定义JSON对象?

来自分类Dev

如何在http 404 ASP.NET Web API中返回自定义Json

来自分类Dev

如何在 WEB.API 中显示自定义错误 JSON 结果

来自分类Dev

如何在用于.NET 4的Web API 4中具有自定义API URL

来自分类Dev

如何在ASP.NET Web API 2中自定义对我自己的表集的身份验证?

来自分类Dev

Web API中的自定义IAuthenticationFilter和AllowAnonymous

来自分类Dev

ASP.NET Web Api中的自定义ParameterBindingAttribute

来自分类Dev

Web音频API中的自定义波形

来自分类Dev

Web Speech API自定义单词

来自分类Dev

Web Speech API自定义单词

来自分类Dev

Web API-自定义路由

来自分类Dev

自定义授权 - Web Api

来自分类Dev

如何在Shopify中创建自定义Web挂钩

来自分类Dev

如何在Web组件中捕获自定义事件?

来自分类Dev

如何使用AWS api网关和Web api实施自定义身份验证(逻辑)

来自分类Dev

如何在Java Web App中创建自定义标签?如何在JSP中使用自定义标签?

来自分类Dev

如何在Java Web App中创建自定义标签?如何在JSP中使用自定义标签?

来自分类Dev

如何将服务注入自定义ActionFilterAttribute(Web API)?

来自分类Dev

如何使用C#将自定义对象发布到Web API

来自分类Dev

Web API帮助页面-自定义属性文档

来自分类Dev

Autofac注入到自定义Web-Api FilterAttribute

来自分类Dev

从Web Api反序列化自定义异常

来自分类Dev

使用自定义对象调用Web API方法

来自分类Dev

Web API AuthorizeAttribute不返回自定义响应

来自分类Dev

ASP.NET Web API自定义帮助页面

来自分类Dev

Web API 2自定义身份验证

来自分类Dev

使用自定义授权测试异步Web API方法

来自分类Dev

ASP.net Web API 2自定义方法

来自分类Dev

Web Api自定义授权属性属性

Related 相关文章

  1. 1

    如何在ASP.NET Framework Web API中返回自定义JSON对象?

  2. 2

    如何在http 404 ASP.NET Web API中返回自定义Json

  3. 3

    如何在 WEB.API 中显示自定义错误 JSON 结果

  4. 4

    如何在用于.NET 4的Web API 4中具有自定义API URL

  5. 5

    如何在ASP.NET Web API 2中自定义对我自己的表集的身份验证?

  6. 6

    Web API中的自定义IAuthenticationFilter和AllowAnonymous

  7. 7

    ASP.NET Web Api中的自定义ParameterBindingAttribute

  8. 8

    Web音频API中的自定义波形

  9. 9

    Web Speech API自定义单词

  10. 10

    Web Speech API自定义单词

  11. 11

    Web API-自定义路由

  12. 12

    自定义授权 - Web Api

  13. 13

    如何在Shopify中创建自定义Web挂钩

  14. 14

    如何在Web组件中捕获自定义事件?

  15. 15

    如何使用AWS api网关和Web api实施自定义身份验证(逻辑)

  16. 16

    如何在Java Web App中创建自定义标签?如何在JSP中使用自定义标签?

  17. 17

    如何在Java Web App中创建自定义标签?如何在JSP中使用自定义标签?

  18. 18

    如何将服务注入自定义ActionFilterAttribute(Web API)?

  19. 19

    如何使用C#将自定义对象发布到Web API

  20. 20

    Web API帮助页面-自定义属性文档

  21. 21

    Autofac注入到自定义Web-Api FilterAttribute

  22. 22

    从Web Api反序列化自定义异常

  23. 23

    使用自定义对象调用Web API方法

  24. 24

    Web API AuthorizeAttribute不返回自定义响应

  25. 25

    ASP.NET Web API自定义帮助页面

  26. 26

    Web API 2自定义身份验证

  27. 27

    使用自定义授权测试异步Web API方法

  28. 28

    ASP.net Web API 2自定义方法

  29. 29

    Web Api自定义授权属性属性

热门标签

归档