添加按钮以通知运行函数的通知
Add buttons to libnotify notifications that run functions
我需要在libnotice通知的底部添加按钮,以便在单击时运行功能。我可以显示按钮,但它们在单击时不会运行这些功能。它根本不提供任何错误消息。
该程序的调用方式为./notifications "Title" "Body" "pathtoicon"
法典:
#include <libnotify/notify.h>
#include <iostream>
void callback_mute(NotifyNotification* n, char* action, gpointer user_data) {
std::cout << "Muting Program" << std::endl;
system("pkexec kernel-notify -am");
}
int main(int argc, char * argv[] ) {
GError *error = NULL;
notify_init("Basics");
NotifyNotification* n = notify_notification_new (argv[1],
argv[2],
argv[3]);
notify_notification_add_action (n,
"action_click",
"Mute",
NOTIFY_ACTION_CALLBACK(callback_mute),
NULL,
NULL);
notify_notification_set_timeout(n, 10000);
if (!notify_notification_show(n, 0)) {
std::cerr << "Notification failed" << std::endl;
return 1;
}
return 0;
}
任何帮助将不胜感激,谢谢!
你必须使用GMainLoop
,一个"主事件循环"才能使回调函数工作。libnotify
使用此循环来处理其操作,如果没有它,它根本不会调用您期望的回调函数,因为没有任何处理它。
基本上在你的main
函数中,只需在开始时添加一个GMainLoop *loop
,然后loop = g_main_loop_new(nullptr, FALSE);
初始化它,最后只需添加g_main_loop_run(loop);
。您的程序应该像以前一样运行,但回调函数现在可以工作了。所以基本上:
int main(int argc, char **argv)
{
GMainLoop *loop;
loop = g_main_loop_new(nullptr, FALSE);
// ... do your stuff
g_main_loop_run(loop);
return 0;
}
您可以在以下位置阅读有关它的更多信息:主事件循环:GLib 参考手册
您不必包含glib.h
因为无论如何libnotify
都包含它。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数如何通知用户它基于函数原型抛出异常?
- 如何声明从 QApplication 派生的 Qt 类并覆盖通知函数?
- 添加按钮以通知运行函数的通知
- 是否有任何常规方法可以通知 STL 移动和复制构造函数?
- 当我使用函数combobox_setcursel时,cbn_selchange通知不起作用
- 用信号通知线程启动特定的函数
- C++:使用回调函数作为事件通知程序
- 调用"事件:<float>:通知(NULL, const double&)"没有匹配函数|