Event to signal
-
Hi guys,
i've try to use event to signal feature but take an errors.Header-only feature class EventsToSignals.hpp:
#ifndef EVENTSTOSIGNALS_H #define EVENTSTOSIGNALS_H #include <QObject> #include <QEvent> class EventsToSignals : public QObject { Q_OBJECT const QEvent::Type type; public: explicit EventsToSignals(QObject*parent, QEvent::Type t = QEvent::None ) : QObject(parent), type(t) { parent->installEventFilter(this); } signals: void onEvent(QObject *watched, QEvent *event); private: bool eventFilter(QObject *watched, QEvent *event) { bool handled = QObject::eventFilter(watched, event); if( type==QEvent::None || type==event->type() ) emit onEvent(watched, event); return handled; } }; template<typename ... Args > auto connectEvent(QObject *obj, QEvent::Type t, Args ... args) { return obj->connect( new EventsToSignals(obj, t), &EventsToSignals::onEvent, args... ); } template<typename ... Args > auto connectEvent(QObject *obj, Args ... args) { return obj->connect( new EventsToSignals(obj), &EventsToSignals::onEvent, args... ); } #endif // EVENTSTOSIGNALS_H
Trying to use main.cpp:
#include <QApplication> #include <QComboBox> #include <QDebug> #include <QAbstractItemView> #include "EventsToSignals.hpp" int main(int argc, char *argv[]) { QApplication a(argc, argv); QComboBox w; w.addItems({"a", "bb", "cccc", "ddddd"}); connectEvent( w.view(), QEvent::Show, [](QObject /*watched*/, QEvent /*event*/){ qDebug()<<"Show popo-up view"; }); w.show(); return a.exec(); }
..\..\EventToSignal\example\../src/EventsToSignals.hpp:34:90: required from 'auto connectEvent(QObject*, QEvent::Type, Args ...) [with Args = {qMain(int, char**)::<lambda(QObject, QEvent)>}]' ..\..\EventToSignal\example\main.cpp:16:6: required from here C:\Qt\5.15.2\mingw81_32\include/QtCore/qglobal.h:121:63: error: static assertion failed: Signal and slot arguments are not compatible. # define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message) ^~~~~~~~~~~~~~~ C:\Qt\5.15.2\mingw81_32\include/QtCore/qobject.h:328:9: note: in expansion of macro 'Q_STATIC_ASSERT_X' Q_STATIC_ASSERT_X((FunctorArgumentCount >= 0),
Help please!
-
@Alexey-Serebryakov said in Event to signal:
(QObject /watched/, QEvent /event/)
Don't know if this is thea reason but this signature is wrong. There signal does not emit objects but pointers.
-
@Alexey-Serebryakov said in Event to signal:
(QObject /watched/, QEvent /event/)
Don't know if this is thea reason but this signature is wrong. There signal does not emit objects but pointers.
@Christian-Ehrlicher Oh! Sorry, my mistakes! :-) Thanks a lot.