I want to connect a signal which is created by a C++ QObject to a function in a QML item. The signal is the "activated" signal from a QSystemTrayIcon and its argument is ActivationReason (an enum value).
Unfortunately it seems I can't connect a signal with this signature to a slot which seems to only be able to receive QVariant.
In the QML file
function trayIconClicked(reason) { ... }
In the C++ code
QObject::connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), rootObject, SLOT(trayIconClicked(QVariant)));
And this is what I get
QObject::connect: Incompatible sender/receiver arguments
QSystemTrayIcon::activated(QSystemTrayIcon::ActivationReason) --> ApplicationWindow_QMLTYPE_11_QML_60::trayIconClicked(QVariant)
If I completely drop the argument on the slot side, the event calls the function. But then I have no idea what ActivationReason triggred it.
A full working version of the code above is here, except that I can't hand in the reason parameter https://github.com/sturmf/qt_samples/tree/master/trayicon
I now subclassed the QSystemTrayicon and added a slot which emits another signal without the parameter.
#ifndef TRAYICON_H
#define TRAYICON_H
#include <iostream>
#include <QObject>
#include <QSystemTrayIcon>
class TrayIcon : public QSystemTrayIcon
{
Q_OBJECT
public:
explicit TrayIcon(QObject *parent = 0) : QSystemTrayIcon(parent){}
signals:
void triggered();
public slots:
void trayIconActivated(QSystemTrayIcon::ActivationReason reason) {
std::cout << "activated" << std::endl;
if (reason == QSystemTrayIcon::Trigger) {
std::cout << "tiggered" << std::endl;
emit triggered();
}
}
};
#endif // TRAYICON_H
In Qt5 you can connect to an anonymous function, by the way.
QObject::connect(trayIcon
, SIGNAL(activated(QSystemTrayIcon::ActivationReason))
, [=](QSystemTrayIcon::ActivationReason r){
rootObject->trayIconClicked(r);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.