1

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 と同様のままにします。

ウェブ通知

4

2 に答える 2

3

でカスタマイズNotifications Web APIするにQtWebkitは、「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フォルダを作成

  • 通知フォルダーに通知クラスを配置します。

    通知.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
    

    通知.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.pri_src.pro

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

コンパイル/ビルド:

  • src.proQtCreator で開く
  • Build(リリースモードで)クリックします(または + を使用しCtrlますB)ボタン(ボタンをクリックしないでください、 +を使用しないでください)Run CtrlR
  • 近い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.dllしますQtDir/plugins/webkit/mywebkitplugind.dll(例: mingw: を使用C:/qt/qt5.4/mingw/plugin/webkit/mywebkitplugind.dll)
  • webkitフォルダが存在しない場合は作成します。
  • プロジェクトを開き、QWebViewテストしNotification Web APIます。

を使用するプロジェクトを実行するQWebViewと、自動的にロードされdll(プロジェクトに余分な構成は必要ありません)、「カスタム ウィジェット」のデフォルトNotifications( QtWebkitWindows ではSystemTrayIconshowに使用されます) が「置換」されます。Notification Web API

プラグイン プロジェクトのフォルダ構造:

mywebkitplugin
├── `src.pro`
├── mywebkitplugin.h
├── mywebkitplugin.cpp
└── notification
    ├── notification.h
    ├── notification.cpp
    └── `notification.pri`
于 2015-03-11T16:40:23.840 に答える