Results 1 to 8 of 8

Thread: Inactivity tracker in QWidget

  1. #1
    Join Date
    May 2015
    Posts
    35
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Inactivity tracker in QWidget

    Hallo everybody,

    I created a widget with really a lot of elements of all kinds. All well sorted in layouts. The GUI has a central layout. Runs good. Now I want to close that widget automatically if the user goes cruising for burgers or 6 bud-light down on the corner - i.e. is inactive for - say 5 minutes.
    Of course I would say
    Qt Code:
    1. QTimer *timer = new QTimer(this);
    2. connect(timer, SIGNAL(timeout()), this, SLOT(close()));
    3. timer.start(300000)
    To copy to clipboard, switch view to plain text mode 

    I did

    Qt Code:
    1. void myWidget::mouseMoveEvent(QMouseEvent *e){
    2. qDebug() << "mouse tracked";
    3. resetMyTimer();
    4. }
    To copy to clipboard, switch view to plain text mode 

    which gave me

    Debugging starts
    mouse tracked
    mouse tracked
    mouse tracked
    mouse tracked
    mouse tracked
    Debugging has finished
    All very good. But 99% of myWidget are covered by elements. Therefore mouse-events are hard to gain. How is the elegant way out of here?

    Thanks and Cheers, Lars


    Added after 4 minutes:


    as always forgot to register for replies...
    Last edited by mustermann.klaus@gmx.de; 28th April 2019 at 00:04.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,294
    Thanks
    36
    Thanked 1,523 Times in 1,474 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Inactivity tracker in QWidget

    You can install an event filter in the QApplication object.
    See QObject::eventFilter().

    It will "see" all events independent of the actual receiver.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    mustermann.klaus@gmx.de (29th April 2019)

  4. #3
    Join Date
    May 2015
    Posts
    35
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Inactivity tracker in QWidget

    Hey anda_skoa,

    Thumbs up!

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow w;
    5. a.installEventFilter(&w);
    6. w.show();
    7.  
    8. return a.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    In MainWindow:

    Qt Code:
    1. bool MainWindow::eventFilter(QObject *obj, QEvent *event){
    2.  
    3. if (event->type() == QEvent::MouseMove) {
    4. qDebug() << "Mouse filter tracked";
    5. return true;
    6. }
    7. else{
    8. return false;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    very clearly trow:

    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Mouse filter tracked
    Question, because I never used it: is it very expensive to do so?

    Cheers, Lars

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,977
    Thanks
    232
    Thanked 628 Times in 618 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Inactivity tracker in QWidget

    Question, because I never used it: is it very expensive to do so?
    Not compared to a user's typical response time. Mouse signals are being propagated everywhere as your app runs and decisions being made - which widget is active, does it process the event or does its parent, map from global coordinates to widget coordinates, etc., etc. One more filter will add negligible delay.

    Anda_skoa suggested installing the event filter on the QApplication instance. If you install it on the MainWindow as you have done, mouse events when a modal QDialog is active will not be seen by your filter, but would be seen by a filter installed on the application.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    mustermann.klaus@gmx.de (29th April 2019)

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,294
    Thanks
    36
    Thanked 1,523 Times in 1,474 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Inactivity tracker in QWidget

    Quote Originally Posted by d_stranz View Post
    Anda_skoa suggested installing the event filter on the QApplication instance. If you install it on the MainWindow as you have done, mouse events when a modal QDialog is active will not be seen by your filter, but would be seen by a filter installed on the application.
    I was confused a bit as well, but actually this is correct.
    The MainWindow object is the event filter and it is installed on the application object.

    What I would suggest to do differently though is to always return "false".
    I.e. the event filter is only "watching" move events, it doesn't "consume" them.

    Cheers,
    _

  8. #6
    Join Date
    May 2015
    Posts
    35
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Inactivity tracker in QWidget

    Thanks for the replies. That's right d_stranz. But hovering over the modal window if open generates events. That would be sufficient for me. Anyway, just for the understanding, is that what you meant: ?
    Qt Code:
    1. int main(int argc, char *argv[]){
    2.  
    3. QApplication a(argc, argv);
    4.  
    5. MainWindow w;
    6. w.installEventFilter(&a);
    7. w.show();
    8.  
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    If so, where do I locate the SLOT, which I modified as follows:
    Qt Code:
    1. // track user-interaction
    2. bool MainWindow::eventFilter(QObject *obj, QEvent *event){
    3.  
    4. // check for specific content of "event" and react
    5. if (event->type() == QEvent::KeyPress || event->type() == QEvent::MouseMove){
    6. activityHappened = true;
    7. }
    8. // pass event to parent for further use
    9. return QMainWindow::eventFilter(obj, event);
    10. }
    To copy to clipboard, switch view to plain text mode 

    To me as a non prof that's a little confusing. Sorry again for that.


    Thanks so much. Regards, Lars

  9. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,977
    Thanks
    232
    Thanked 628 Times in 618 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Inactivity tracker in QWidget

    The MainWindow object is the event filter and it is installed on the application object.
    Ah, right you are. I misread the "a" for a "w". @mustermann - your original code is correct, my mistake.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. The following user says thank you to d_stranz for this useful post:

    mustermann.klaus@gmx.de (29th April 2019)

  11. #8
    Join Date
    May 2015
    Posts
    35
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Inactivity tracker in QWidget

    Feels good to hear that. As always thanks so much for the support.

    Lars

Similar Threads

  1. Robot's Tracker Application
    By SancheZZY in forum Qt Programming
    Replies: 1
    Last Post: 30th April 2017, 19:36
  2. XYZ tracker for QwtPlotSpectrogram
    By epsilon in forum Qwt
    Replies: 2
    Last Post: 5th September 2012, 04:53
  3. Lock screen after inactivity time
    By baobui in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 20th March 2011, 22:18
  4. howto get value at Tracker Point
    By pospiech in forum Qwt
    Replies: 1
    Last Post: 9th February 2009, 10:53
  5. R6 Bill Tracker
    By croland in forum Qt-based Software
    Replies: 0
    Last Post: 16th October 2007, 20:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.