Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Connecting QApplication event with QMainWindow instance
Qt 6.12 LTS is out! See what's new in the release blog

Connecting QApplication event with QMainWindow instance

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.0k 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.
  • X Offline
    X Offline
    xtingray
    wrote on last edited by xtingray
    #1

    Hi,
    I found this interesting example in the Qt documentation:

    #include <QApplication>
    #include <QFileOpenEvent>
    #include <QtDebug>
    
    class MyApplication : public QApplication
    {
    public:
        MyApplication(int &argc, char **argv)
            : QApplication(argc, argv)
        {
        }
    
        bool event(QEvent *event)
        {
            if (event->type() == QEvent::FileOpen) {
                QFileOpenEvent *openEvent = static_cast<QFileOpenEvent *>(event);
                qDebug() << "Open file" << openEvent->file();
            }
    
            return QApplication::event(event);
        }
    };
    

    Now, in my case I have a QMainWindow instance that I want to connect with the QFileOpenEvent. Either MyApplication object and the QMainWindow object are defined in the main.cpp file.

    What is the best way to connect those objects when MyApplication::event() method is triggered?

    Thanks!


    Qt Developer

    K 1 Reply Last reply
    0
    • X xtingray

      Hi,
      I found this interesting example in the Qt documentation:

      #include <QApplication>
      #include <QFileOpenEvent>
      #include <QtDebug>
      
      class MyApplication : public QApplication
      {
      public:
          MyApplication(int &argc, char **argv)
              : QApplication(argc, argv)
          {
          }
      
          bool event(QEvent *event)
          {
              if (event->type() == QEvent::FileOpen) {
                  QFileOpenEvent *openEvent = static_cast<QFileOpenEvent *>(event);
                  qDebug() << "Open file" << openEvent->file();
              }
      
              return QApplication::event(event);
          }
      };
      

      Now, in my case I have a QMainWindow instance that I want to connect with the QFileOpenEvent. Either MyApplication object and the QMainWindow object are defined in the main.cpp file.

      What is the best way to connect those objects when MyApplication::event() method is triggered?

      Thanks!

      K Offline
      K Offline
      kenchan
      wrote on last edited by
      #2

      @xtingray
      Did you try an EventFilter on either of those objects?

      X 1 Reply Last reply
      1
      • K kenchan

        @xtingray
        Did you try an EventFilter on either of those objects?

        X Offline
        X Offline
        xtingray
        wrote on last edited by xtingray
        #3

        @kenchan Reading the documentation you shared with me I found an easy solution adding this line inside the event() method of MyApplication:

                QApplication::sendEvent(mainWindow, event);
        

        Thanks!


        Qt Developer

        1 Reply Last reply
        1

        • Login

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