PDA

View Full Version : QFileOpenEvent connecting to QMainWindow



BrianNorman
16th May 2016, 16:28
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

d_stranz
16th May 2016, 16:52
If you have subclassed QApplication already, then just add a signal to it:



// MyApplication.h

class MyApplication : public QApplication
{

//...
signals:
void openFile( const QString & fileName );

};


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.



// MainWindow.h

class MainWindow : public QMainWIndow
{
// ...

public slots:
void onOpenFile( const QString & fileName );

};

// main.cpp

int main( /* ... */ )
{

MyApplication myApp;
MainWindow myMain;

connect ( &myApp, &MyApplication::openFile, &myMain, &MainWindow::onOpenFile );

myMain.show();
return myApp.exec();
}

BrianNorman
16th May 2016, 17:19
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 );

};

d_stranz
16th May 2016, 23:53
If this is your actual code, then you are missing the Q_OBJECT macro at the start of the class definition:



class MyApplication : public QApplication
{
Q_OBJECT

public:
MyApplication(int &argc, char **argv)
: QApplication(argc, argv)
{
}


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.