我创建使用一个简单的浏览器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 API
,QtWebkit
您必须使用“ Webkit插件”,换句话说,创建一个插件并放入中qtdir/plugins/webkit
。
注意:对于需要创建插件的
<QtWebKit/QWebKitPlatformPlugin>
类
在.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.pri
在src.pro
:
include($$PWD/notification/notification.pri)
src.pro
在QtCreator中打开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
(在项目中不需要额外的配置),并将“自定义小部件” “替换”默认值Notifications
(QtWebkit
在Windows中SystemTrayIcon
用于show Notification Web API
)。
mywebkitplugin
├── `src.pro`
├── mywebkitplugin.h
├── mywebkitplugin.cpp
└── notification
├── notification.h
├── notification.cpp
└── `notification.pri`
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句