如何定制" Notification Web API"在QT

时间:2015-03-03 04:48:49

标签: c++ qt qtwebkit qwebpage

我正在使用QtWebkit创建一个简单的浏览器,我设法使用QWebPage::setFeaturePermission添加对Notification Web API的支持。

示例:

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";
    }
}

每次点击&#34;通知我&#34; 按钮,桌面上都会显示以下消息:

Desktop Notification

可以在QT中自定义通知吗?换句话说,类似于GoogleChrome或Firefox,就像这样:

Web Notification

2 个答案:

答案 0 :(得分:3)

要在Notifications Web API中自定义QtWebkit,您必须使用&#34; Webkit插件&#34;,换句话说,创建一个插件并放入qtdir/plugins/webkit

  

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

创建插件:

  • 在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* dataJavaScript API获取数据。

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

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

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

编译/构建:

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

运行使用QWebView的项目时,它会自动加载dll(您的项目中不需要额外配置),并且#34;替换&#34; Windows中的默认NotificationsQtWebkit使用SystemTrayIcon for show Notification Web API)为您的&#34;自定义小部件&#34;。

插件项目的文件夹结构:

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

答案 1 :(得分:0)

新通知可以采用以下两个参数:

var notification = new Notification(title, options);

作为选项对象的一部分,您可以传递&#39; body&#39;和&#39;图标&#39;显示在通知中。