QT Notifications Chapter 1 – Installing Snorenotify

Hello, it’s been a while to not post about any topic. Right now I’ve found a new content for myself to talk about. It’s notifications.

Firstly I’ll talk about QT’s QSystemTrayIcon class, then explain why it’s not enough.

Let’s dive into QSystemTrayIcon. Here is a link for my dummy sample.

    setIcon(QIcon(":/rocket-icon.png"));
    setVisible(true);
    showMessage("GOOL","BUCASPOR ATTI", QIcon(":/rocket-icon.png"), 20000);
    connect(this, &SystemTray::messageClicked, this, &SystemTray::handleMessageClicked);

showMessage method display’s system wide notification and connect connects whenever it is clicked to handler slot. (I know connect should be before showing message, I missed that one can fix repo later).

Problem is messageClicked() doesn’t pass a parameter, when it’s emitted. This causes an issue of unknown. I simply can’t know which of the notification I’ve clicked, if there is multiple notification from my project.

I’ve also dived into the Qt’s source.

void QBalloonTip::mousePressEvent(QMouseEvent *e)
{
    close();
    if(e->button() == Qt::LeftButton)
        emit trayIcon->messageClicked();
}

It just emits an empty signal. It’s ok, if a developer wants to notify only information and doesn’t want to redirect it to anywhere.

Imagine you created an app like Whatsapp! The thing is multiple users can message to another user at the same time. If you use Qt’s QSystemTray, then it’s useless.

Sooooo, what should we do as developers? KDE Goddess Hannah (Thank you!!!) has Snore Notify! It’s also Qt based cross platform notification plugin. + it can returns the notification object and it’s possible to reach it’s unique id. Sounds great!

Installation

At first it’s GitHub repo is actually so clear! But do not forget to install ECM = Extra CMake Modules of KDE. Snore Notify needs for it. Also Windows is target platform and intended to use only operating system’s notification system, then you also should install KDE/snoretoast too!!!

Also last pipeline of master branch from Snorenotify seems failed. I’ve found out the reason is related about precompiler for Mac OS. plugincontainer.cpp : QStringList list variable has declared on line 167 and if OS is Mac it’s used in line 157 which is not declared! Here is a quick fix just replace const QDir &PluginContainer::pluginDir() method from my gist. I’m also going to contribute this fix and a few other things to the repository of KDE too.

Installing ECM- Extra-CMake-Modules

You can install from CMake’s GUI or using it by QT Creator’s “Project” option. If you use QT Creator IDE do not forget to change your target to install!

Installing Snorenotify

In this part do not install it with CMake! use your QT Creator IDE to fix whole environment. For Windows users it should use MSVC 2017 instead of 2019!

First condition is to set ECM_DIR parameter. Add it as directory and set it.

For BUILD options I intend to not build BUILD_COVERAGE and BUILD_SHARED_LIBS. You can install on Linux, Windows and Mac with this way.

For Windows, please do not forget to download KDE’s Snoretoast too and merge it with the installation of Snorenotify for Windows system notifications.

Snorenotify also has a generic notifications backend named as “Snore”. If you want to use Snore you should update notification.qml file. Update import versions and change Connections callback from:

Connections{
            target: notifyWidget
            onPositionChanged: root.updatePosition()
        }
Connections{
            target: notifyWidget
            function onPositionChanged(){
                root.updatePosition()
           }
        }

Callback handlers are deprecated should be called as function instead of a property method.

One last thing I’ve noticed that on Snorenotify’s test code. includes libsnore/snoreconstants.h. This header file is not copied at installation time of CMake. You can add a CMake step, if you want to make an automation for this plugin or simply copy it from source to your installation path. It’s just a simple header contains a few properties.

#ifndef SNORECONSTANTS_H
#define SNORECONSTANTS_H

#include "snoreglobals.h"

#include <QString>

namespace Snore {
namespace Constants {
namespace SettingsKeys {
static const SettingsKey PrimaryBackend = {QStringLiteral("PrimaryBackend"), GlobalSetting};
static const SettingsKey Timeout = {QStringLiteral("Timeout"), GlobalSetting};
static const SettingsKey Silent = {QStringLiteral("Silent"), LocalSetting};
static const SettingsKey Enabled = {QStringLiteral("Enabled"), LocalSetting};
static const SettingsKey PluginTypes = {QStringLiteral("PluginTypes"), LocalSetting};

}

}
}

#endif // SNORECONSTANTS_H

Directly copy it under include/libsnore/snoreconstants.h

In the next chapter I’ll share a sample project and talk about usage of Snorenotify.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *