【问题标题】:How to customize "Notification Web API" in the QT如何在 QT 中自定义“Notification Web API”
【发布时间】:2015-03-03 04:48:49
【问题描述】:

我正在使用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";
    }
}

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

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

【问题讨论】:

    标签: c++ qt qtwebkit qwebpage


    【解决方案1】:

    要在QtWebkit 中自定义Notifications Web API,您必须使用“Webkit 插件”,即创建一个插件并放入qtdir/plugins/webkit

    注意:创建插件需要&lt;QtWebKit/QWebKitPlatformPlugin&gt;

    创建插件:

    • 在 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文件夹

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

      通知.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
      
    • src.pro中添加notification.pri

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

    编译/构建:

    • 在 QtCreator 中打开 src.pro
    • 点击Build(在释放模式下)(或使用Ctrl+B)按钮(不要点击在@987654344 @按钮,不要使用 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(在你的项目中不需要额外的配置)并且会“替换”默认的NotificationsQtWebkit在Windows中使用SystemTrayIcon为您的“自定义小部件”显示 Notification Web API)。

    插件项目的文件夹结构:

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

    【讨论】:

      【解决方案2】:

      新的通知可以有以下两个参数:

      var notification = new Notification(title, options);
      

      作为选项对象的一部分,您可以传递要在通知中显示的“正文”和“图标”。

      【讨论】:

      • 您在哪里读到我的问题出在 JavaScript 上?我建议在回答任何问题之前阅读整个问题。通知完美运行,我问的是如何自定义通知生成的widget,换句话说,我想将systemtray widget更改为custom widget。对不起诚意(我希望不要冒犯),但他们的回应尝试似乎是“猎人积分”,而不是试图提供帮助。再次对不起诚意,祝你有美好的一天。
      • 对不起,我的回答不完整,因为它没有解决完整的问题,只指出了可能性,应该把它作为评论。
      • 再次抱歉,但您的回答没有解决任何被问到的问题(即使是评论)。在标题中,我已经明确表示这个问题是关于“QT”的,也是有问题的。我希望很明显qtwebkitNotification Web API 完美配合,问题即将用“自定义小部件”替换default notification(QT 中默认使用SystemTray。感谢您阅读我的评论:)
      猜你喜欢
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 2012-04-05
      相关资源
      最近更新 更多