Looking for a Complte Example of subclass QApplication
-
I try to subclass QApplication, so could override x11EventFilter to see if I could capture all mouse and keyboard events under XWindow in Linux. I have the following code
Application.h:
@
class MyApplication : public QApplication
{public:
MyApplication(int argc, char* argv[]);};
@Application.cpp:
@
#include "Application.h"MyApplication::MyApplication(int argc, char ** argv):
QApplication(argc,argv)
{}
@Main.cpp
@
#include "Application.h"
#include "widget.h"int main(int argc, char* argv[]) {
MyApplication a(argc,argv);
Widget w;
a.setActiveWindow(&w);
w.show();
return a.exec();}
@Program does compile, but when it executed it give me segmenant fault error.
Basicly I am looking for working example on how to subclass QApplication, so it does not cause a segmenant fault error.
EDIT: please use @-tags for the code, gerolf
-
Hi,
I don't know where you seg fault comes from, did you tryx to debug it?
You should remove a.setActiveWindow(&w);, this is not needed . Also, this function is a static function, that is used from platform specific event handlers (see in the "docs":http://doc.qt.nokia.com/latest/qapplication.html#setActiveWindow ), which means, it's called from events, but event processing starts with a.exec(). Try to remove it and see, whether it works then. I see no obvious other errors.
so main code should be:
@
#include "Application.h"
#include "widget.h"int main(int argc, char* argv[]) {
MyApplication a(argc,argv);
Widget w;
w.show();
return a.exec();}
@ -
The segmenant happens in function "void QWidgetPrivate::create_sys(WId window, bool initializeWindow, bool destroyOldWindow)", when looking at the function look for the following code:
" XSetWMProperties(dpy, id, 0, 0,
qApp->d_func()->argv, qApp->d_func()->argc,
&size_hints, &wm_hints, &class_hint);
"FYI: I am using VirtualBox not sure if that would cause any issues. If I just do the following:
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();Application launches with no issues.
-
The signature of your MyApplication constructor must be:
@
MyApplication(int &argc, char **argv);// wrong:
//MyApplication(int argc, char **argv);
@Watch out for the reference of argc. With that constructor my own QApplication subclass works without problems.
In your case (without reference) the compiler generates a temporary object (int) for the first argument of your MyApplication constructor. You pass this temporary int to QApplication's constructor (with int reference), which saves the address of this reference. Once your MyApplication constructor is done the temporary int is destroyed but QApplication still has its address. The crashing function calls
@
XSetWMProperties(
dpy,
id,
0,
0,
qApp->d_func()->argv,
qApp->d_func()->argc,
&size_hints,
&wm_hints,
&class_hint
);
@The recently invalidated argc is accessed there and causes the crash.
The "QApplication constructor":http://doc.qt.nokia.com/4.7/qapplication.html#QApplication warns you about this:
bq. Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string.
BTW: It is always a good way to start off by exactly copying the signatures of the base class you inherit from. Only change these if you really need to (using copy 'n paste from the docs saves you some time consuming typo errors :-) )
-
[quote author="Volker" date="1296342188"]The signature of your MyApplication constructor must be:[/quote]
Which is something I already said (see my 1st reply) and the fact OP replied to me tricked me into thinking that he had already fixed that but it wasn't the problem.
I'm really considering to stop replying to people which don't help themselves by listening to us.
-
Hey peppe, sorry for not looking at your post more closely.
After my second post, I do some searching on the web to see if there is a way to hook all of the keyboard and mouse events, I come across an example of QApplicaiton being subclassing, and it does show that when subclass QApplication the passing of "int argc" must be by reference.
-
hook all of the keyboard and mouse events under MAC and Linux
-
Does "Installing an event filter":http://doc.qt.nokia.com/latest/qobject.html#installEventFilter on the QApplication not work for you?