Results 1 to 4 of 4

Thread: QFileOpenEvent connecting to QMainWindow

  1. #1
    Join Date
    Jan 2016
    Posts
    17
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default QFileOpenEvent connecting to QMainWindow

    Looking into auto file open on a a Mac.

    I have a standard Qt app where I have subclassed QApplication adding an event filter. Here I capture the QEvent::FileOpen and I can debug message the filename. All good.

    What I want to do now is to connect a signal to my MainWindow to tell it to do something with the filename. I thought the route would be to connect a signal from the my QApplication to the MainWindow but I am struggling to do this.

    Any ideas?

    Thanks

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileOpenEvent connecting to QMainWindow

    If you have subclassed QApplication already, then just add a signal to it:

    Qt Code:
    1. // MyApplication.h
    2.  
    3. class MyApplication : public QApplication
    4. {
    5.  
    6. //...
    7. signals:
    8. void openFile( const QString & fileName );
    9.  
    10. };
    To copy to clipboard, switch view to plain text mode 

    and in your event handler, emit that signal with the file name after you have verified that the file is of the right type for your app, etc.

    In your MainWindow class, add a public slot and connect to it in main(). Implement the slot in MainWindow.cpp to do whatever it needs to do.

    Qt Code:
    1. // MainWindow.h
    2.  
    3. class MainWindow : public QMainWIndow
    4. {
    5. // ...
    6.  
    7. public slots:
    8. void onOpenFile( const QString & fileName );
    9.  
    10. };
    11.  
    12. // main.cpp
    13.  
    14. int main( /* ... */ )
    15. {
    16.  
    17. MyApplication myApp;
    18. MainWindow myMain;
    19.  
    20. connect ( &myApp, &MyApplication::openFile, &myMain, &MainWindow::onOpenFile );
    21.  
    22. myMain.show();
    23. return myApp.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2016
    Posts
    17
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: QFileOpenEvent connecting to QMainWindow

    Thanks for your prompt reply.

    I was trying this route but where I get stuck is when I add the emit line to the code. I get an error

    error: symbol(s) not found for architecture x86_64

    Any ideas where I am going wrong here?

    Thanks

    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 in QApplication" << openEvent->file();

    emit openFile(openEvent->file());
    }

    return QApplication::event(event);
    }

    signals:
    void openFile( const QString & fileName );

    };

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QFileOpenEvent connecting to QMainWindow

    If this is your actual code, then you are missing the Q_OBJECT macro at the start of the class definition:

    Qt Code:
    1. class MyApplication : public QApplication
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MyApplication(int &argc, char **argv)
    7. : QApplication(argc, argv)
    8. {
    9. }
    To copy to clipboard, switch view to plain text mode 

    Please use [code] tags when posting source code, not [quote] tags. When you post, click "Go Advanced", then click the "#" icon to insert the code tags. Paste your code between them. And once you know what they look like, you can simply type them if it is more convenient.

Similar Threads

  1. Replies: 3
    Last Post: 13th November 2011, 08:12
  2. Strange issue with QFileOpenEvent on MacOS
    By Andrey Saenko in forum Qt Programming
    Replies: 1
    Last Post: 29th August 2011, 16:50
  3. Replies: 2
    Last Post: 29th June 2011, 15:45
  4. Replies: 0
    Last Post: 17th November 2010, 17:07
  5. QFileOpenEvent on Gnome
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2007, 21:28

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.