LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Blogs > rainbowsally
User Name
Password

Notices

Rate this Entry

GUI Programming: Exposing the main event loop (Subclassing QApplication)

Posted 05-02-2012 at 06:23 AM by rainbowsally
Updated 05-02-2012 at 06:25 AM by rainbowsally

GUI Programming: Exposing the main event loop (Subclassing QApplication)

Today's features
  • a QTextEdit in 4 lines of code.
  • subclassing QApplication to view ALL objects and events as they are processed.
  • how to preempt events in QT

It looks like this is about the only way to actually preempt messages in QT4.

This is NOT the same as installEventFilter() which is simple and useful for many things but runs too late in a series of dispatches to preempt or change anything.

Also, this is not the same as the X11 event handler (which is pretty cool if you're into that sort of thing).

And this is NOT just sublcassing the notify handler, which I wonder.. did that EVER work?


This replaces the root dispatcher (notify()) for the QT system by subclassing QApplication and overwriting that method.

If you've discovered the difficulty of subclassing QApplication here yuh go. Just a dash of Computer Mad Science and voila! :-)


file: src/main.cpp
Code:
#include <QApplication> // app
#include <QTextEdit>    // editor
#include <QKeyEvent>    // QKeyEvent
// #include "utils.h"      // qkey2name(), handle_return_key(QTextEditor*), etc.
#include <stdio.h>      // printf()

QObject* target;

void dbg(){}
class LQApplication : public QApplication
{
public:
  LQApplication(int &argc, char **argv);
  ~LQApplication();
  bool notify(QObject *receiver, QEvent *e)
  { 
    printf("receiver: %s0x%lX: eventType: %d\n", 
           receiver == target ? "TARGET -> " : "", 
           (long)receiver, e->type());
    return QApplication::notify(receiver, e);
  }
};

LQApplication::LQApplication(int &argc, char **argv)
: QApplication(argc, argv)
{
  // additional setup
}

LQApplication::~LQApplication()
{
  // delete additional setup as req'd
}

////////////////////////////////////////////////////
// standard-ish main except that it adds a TextEdit 
// set to all the defaults.
int main(int argc, char *argv[])
{
  dbg();
  LQApplication app(argc, argv);
  QTextEdit editor;
  target = &editor;
  editor.show();
  return app.exec();
}

Here's a snip from what it dumped right as I closed the window.

Code:
receiver: 0x81524C0: eventType: 18
receiver: TARGET -> 0xBFFE30F4: eventType: 27
receiver: 0xBFFE3108: eventType: 20
receiver: TARGET -> 0xBFFE30F4: eventType: 77
receiver: 0x80F6F50: eventType: 71
receiver: 0x80FE278: eventType: 16
receiver: 0x80F6E68: eventType: 71
receiver: 0x80FAC18: eventType: 16
receiver: 0x80F6E68: eventType: 16
receiver: 0x80FDFA8: eventType: 71
receiver: 0x80FB4F0: eventType: 16
receiver: 0x80FDFA8: eventType: 16
receiver: 0x8141E18: eventType: 16
receiver: 0x8145120: eventType: 16
receiver: 0x8147A18: eventType: 16
receiver: 0x81524C0: eventType: 16
receiver: TARGET -> 0xBFFE30F4: eventType: 16
It's pretty clear what's happening here with event type 16 even without a name printout utility, since TARGET is getting the message last.

Same with event type 71.

If you want to use qmake to generate the makefile, go for it. But you might actually find mc2 makefile creator even easier, especially now that it has the qt4 template (load using 'mc2 -fetch qt4').

Here's the mc2 link page if you want to check it out.
http://www.linuxquestions.org/questi...eration-34648/

The Computer Mad Science Team

:-)
Posted in Uncategorized
Views 538 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 09:21 PM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration