备忘:Qt如何配置wintoast

  1. 原项目拷下来wintoastlib.hwintoastlib.cpp

  2. 丢进项目目录里分别注册为头文件和源文件。
    如果用CMake愿意也可以注册为外部库。做法是CMake里加上:

add_library(WinToast wintoastlib.cpp wintoastlib.h)
target_link_libraries(WinToast)
  1. 一个widget.cpp的配置例子:
#include "widget.h"
#include "./ui_widget.h"

//Step1. 引入wintoastlib
#include "wintoastlib.h"

//Step2. 构造一个CustomHandler类
class CustomHandler : public WinToastLib::IWinToastHandler {
public:
    void toastActivated() const {
        std::wcout << L"The user clicked in this toast" << std::endl;
    }

    void toastActivated(int actionIndex) const {
        std::wcout << L"The user clicked on button #" << actionIndex << L" in this toast" << std::endl;
    }

    void toastFailed() const {
        std::wcout << L"Error showing current toast" << std::endl;
    }
    void toastDismissed(WinToastDismissalReason state) const {
        switch (state) {
        case UserCanceled:
            std::wcout << L"The user dismissed this toast" << std::endl;
            break;
        case ApplicationHidden:
            std::wcout << L"The application hid the toast using ToastNotifier.hide()" << std::endl;
            break;
        case TimedOut:
            std::wcout << L"The toast has timed out" << std::endl;
            break;
        default:
            std::wcout << L"Toast not activated" << std::endl;
            break;
        }
    }
};

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    //Step3. 按如下方式初始化设置AUMI等以便告知Windows这是个什么程序,内容替换为您需要的
    WinToastLib::WinToast::instance()->setAppName(L"WinToastTest");
    const auto aumi = WinToastLib::WinToast::configureAUMI(L"izwb003", L"wintoast", L"WinToastTest", L"20161006");
    WinToastLib::WinToast::instance()->setAppUserModelId(aumi);
    if (!WinToastLib::WinToast::instance()->initialize()) {
        std::wcout << L"Error, could not initialize the lib!" << std::endl;
    }
    else
        std::cout<<"OK"<<std::endl;

    //Step4. 构建Toast并显示。参考原项目文档
    WinToastLib::WinToastTemplate templ = WinToastLib::WinToastTemplate(WinToastLib::WinToastTemplate::Text02);
    templ.setTextField(L"title", WinToastLib::WinToastTemplate::FirstLine);
    templ.setTextField(L"subtitle", WinToastLib::WinToastTemplate::SecondLine);
    templ.setAudioOption(WinToastLib::WinToastTemplate::Silent);
    const auto toast_id = WinToastLib::WinToast::instance()->showToast(templ, new CustomHandler());
}

Widget::~Widget()
{
    delete ui;
}

以上做法adapted一下即可。

posted @ 2023-09-17 04:53  IZWB-003  阅读(97)  评论(0编辑  收藏  举报