Issues with eventFilter
-
i don't see how this no-op eventFilter should slow down your application...
The issue must be somewhere else in your code.Another hint:
you don't need to call QObject::eventFilter() in every case branch.
Returning false once at the end of the eventFilter has the same effect ;) -
wrote on 10 Jan 2014, 12:19 last edited by
OK, thanks :)
As far as I know the rest is ok as well:
@
class QTGUI_MainWindow : public QMainWindow
{
Q_OBJECTprotected:
bool eventFilter(QObject *obj, QEvent *event);
...
@@
int main(int argc, char *argv[])
{
QApplication program(argc, argv);
...
QTGUI_MainWindow eventFilter;
program.installEventFilter(&eventFilter);
...
@I'm out of ideas ...
-
wrote on 10 Jan 2014, 12:46 last edited by
Since the eventFilter on the QApplication objects generally slows down the event handling (according doc), is there another way to catch all mouse/touch events?
-
yes, you can subclass QApplication and reimplement "notify()":http://qt-project.org/doc/qt-4.8/qapplication.html#notify.
This has the advantage that this gets only called once for each event and not multiple times like the event filter would do since the events will get propagated... -
wrote on 10 Jan 2014, 12:57 last edited by
I did not really understand everything you wrote. Probably because I am not (yet) very experienced with Qt nor with C++ - I am a "C" guy.
Sure I can dig up some more on that ... thanks anyway :) -
Code is a language every programmer understands... so here you go ;)
@
class MyApplication : public QApplication
{
public:
MyApplication(...) : QApplication(...)
{
}virtual bool notify ( QObject * receiver, QEvent * event ) { //same code like in your previous eventFilter() implementation if( event-should-be-filtered-out ) return true; QApplication::notify(receiver,event); }
};
int main(int argc, char *argv[])
{
MyApplication program(argc, argv);
...
QTGUI_MainWindow mainWin;...
}
@ -
wrote on 10 Jan 2014, 14:54 last edited by
Thanks a million!
It works ... and solves my issue!
Responsiveness is back and everything works as supposed.Best regards (from aprox. 500 km west)
-
wrote on 14 Jan 2014, 09:02 last edited by
I am extending it ... and got stuck.
I seem not to be able to call a function that is defined in another class from notify().
What is the correct way to do this? -
[quote author="McLion" date="1389690173"]
I seem not to be able to call a function that is defined in another class from notify().[/quote]
what exactly is preventing you from calling a method of another class?! -
wrote on 14 Jan 2014, 09:10 last edited by
Getting:
@./release\moc_qtgui_mainwindow.o:moc_qtgui_mainwindow.cpp:(.text$_ZN13MyApplication6notifyEP7QObjectP6QEvent[MyApplication::notify(QObject*, QEvent*)]+0x76): undefined reference to `writeToTCOport(QByteArray)'
collect2: ld returned 1 exit status@The other class:
@class QTGUI_MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit QTGUI_MainWindow(QWidget *parent = 0);
~QTGUI_MainWindow();
void writeToTCOport(QByteArray dataBA);
....@ -
in that order:
clean your project
rerun qmake
rebuild project
or more likely i assume your are just calling the method without an object (your mainwindow instance) on or connecting a signal to it, right? (post the code of the call to writeToTCOport() inside the notify() implementation).
if the error remains:
Are the implementations of the QTGUI-MainWindow and MyApplication classes in the same project? Or e.g. loaded in a library? -
wrote on 14 Jan 2014, 09:36 last edited by
Cleaning and rebuilding did not help.
[quote author="raven-worx" date="1389691039"]
... or more likely i assume your are just calling the method without an object (your mainwindow instance) on or connecting a signal to it, right? (post the code of the call to writeToTCOport() inside the notify() implementation).
[/quote]I get:
@..\src/qtgui_mainwindow.h: In member function 'virtual bool MyApplication::notify(QObject*, QEvent*)':
..\src/qtgui_mainwindow.h:86: error: cannot call member function 'void QTGUI_MainWindow::writeToTCOport(QByteArray)' without object@
when I call it like:
@QTGUI_MainWindow::writeToTCOport(sendData);@And calling without QTGUI_MainWindow:: returns the error posted before.
I seem to miss some of the basic c ++ concepts .. -
@error: cannot call member function 'void QTGUI_MainWindow::writeToTCOport(QByteArray)' without object@
says it clearly: you need an object/instance to call it on.The call
@QTGUI_MainWindow::writeToTCOport(sendData);@
would be fine when you define the method "static". You can define a method static if you don't need to access object specific members within it. If that's ok in this method do it.Otherwise i would suggest something like this for example:
@
class MyApplication : public QApplication
{
public:
MyApplication(...) : QApplication(...)
{
}virtual bool notify ( QObject * receiver, QEvent * event ) { //same code like in your previous eventFilter() implementation if( event-should-be-filtered-out ) return true; if( m_MainWindow ) m_MainWindow->writeToTCOport(...); QApplication::notify(receiver,event); } void setMainWindowObject(QTGUI_MainWindow* mainWin) { m_MainWindow = mainWin; } protected: QPointer<QTGUI_MainWindow> m_MainWindow; }; int main(int argc, char *argv[]) { MyApplication program(argc, argv); ... QTGUI_MainWindow mainWin; program.setMainWindowObject(&mainWin); ... }
@
-
wrote on 14 Jan 2014, 10:57 last edited by
Thanks - tried the static solution. Compiles as long as the function is empty. As soon as I add port->write(); like in many other places I get back the same error.
Going to read your second suggestion ... as well as some C++ tutorials ;) -
as i said:
[quote author="raven-worx" date="1389692609"]You can define a method static if you don't need to access object specific members within it.[/quote]i guess "port" is a member of the class?
[quote author="McLion" date="1389697075"]
Going to read your second suggestion ... as well as some C++ tutorials ;)[/quote]yes, good idea. ;)
-
wrote on 10 Mar 2014, 13:00 last edited by
Need to pick this up again.
The following should filter all MouseEvents while !bTouchActive, i.e. the GUI is hidden in my app. With the code below, the GUI still reflects the touches and another object has the focus after the GUI is turned back on. While the GUI is inactive it currently just should debug the cursor position where it has been touched but not "react" on the GUI. Funny enough, it does change the focus to a different object but it does not execute its press/release MouseEvent.
What is wrong in the code below?@class MyApplication : public QApplication
{
public:
MyApplication(int &argc, char **argv ) : QApplication(argc, argv)
{
}virtual bool notify ( QObject *receiver, QEvent *event ) { switch( event->type() ) { case QEvent::MouseButtonPress: case QEvent::MouseButtonRelease: case QEvent::MouseMove: case QEvent::MouseButtonDblClick: { if(bTouchActive) { qDebug("Touch/Mouse event type: %d", event->type()); } else { QMouseEvent *TEvent = static_cast<QMouseEvent *>(event); QPoint cursorPos = TEvent->globalPos(); qDebug("TouchInActive: TouchXpos %d, TouchYpos %d", cursorPos.rx(), cursorPos.ry()); return true; // return with true filters the event } break; } case QEvent::KeyPress: case QEvent::KeyRelease: { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); qDebug("Key %d pressed, Event type: %d", keyEvent->key(), event->type()); break; // break does not filter and forwards the event to notify() } default: break; } QApplication::notify(receiver,event); return false; }
};
@