Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Looking for a Complte Example of subclass QApplication
Qt 6.11 is out! See what's new in the release blog

Looking for a Complte Example of subclass QApplication

Scheduled Pinned Locked Moved General and Desktop
12 Posts 5 Posters 14.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maverick1000
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      Are you sure your segfault happens because of MyApplication class and not something in Widget class?

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        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();

        }
        @

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

          This
          @
          MyApplication(int argc, char* argv[]);
          @
          signature is wrong.

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Maverick1000
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dangelog
              wrote on last edited by
              #6

              Can you paste a complete testcase? That is, a

              • short
              • compileable
              • self-contained

              snippet that shows the problem.

              Software Engineer
              KDAB (UK) Ltd., a KDAB Group company

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                I think it's in the code of your Widget, not in teh application.
                Can you show us your widget code?

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #8

                  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 :-) )

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dangelog
                    wrote on last edited by
                    #9

                    [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.

                    Software Engineer
                    KDAB (UK) Ltd., a KDAB Group company

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Maverick1000
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Maverick1000
                        wrote on last edited by
                        #11

                        hook all of the keyboard and mouse events under MAC and Linux

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          Does "Installing an event filter":http://doc.qt.nokia.com/latest/qobject.html#installEventFilter on the QApplication not work for you?

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved