PDA

View Full Version : Strange issue with QFileOpenEvent on MacOS



Andrey Saenko
29th August 2011, 16:51
I have to subclass QApplication and redefine event function, that waiting for FileOpen event and runs some actions after it comes, and also i created template Info.plist file with extensions of registred filetypes for this application. When i try to open associated file by double mouse click, application have to start and creates main window, but then it crash in QCoreApplication::Arguments and message box from Application::event not executed.


class Application : QApplication
{
public:
Application( int argc, char * argv[]):QApplication(argc,argv){}
protected:
virtual bool event(QEvent * event )
{
if( event->type() == QEvent::FileOpen )
{
QMessageBox box;
box.setText(static_cast<QFileOpenEvent>(event)->file());
box.exec();
return true;
}
return false;
}

}

But if i create messagebox before event type check(as in code below), it works fine, both messagebox are showing. What could be the the reason for this behavior ?


class Application : QApplication
{
public:
Application( int argc, char * argv[]):QApplication(argc,argv){}
protected:
virtual bool event(QEvent * event )
{
QMessageBox msgbox;
msgbox.setText(QString::number(event->type()));
msgbox.exec()
if( event->type() == QEvent::FileOpen )
{
QMessageBox box;
box.setText(static_cast<QFileOpenEvent>(event)->file());
box.exec();
return true;
}
return false;
}

}

yeye_olive
29th August 2011, 17:50
It likely has nothing to do with the declaration of the QMessageBoxes. By systematically returning true or false from your event() reimplementation, your are disrupting all the default treatment of events by the QApplication object. You could try the following:


class Application : QApplication
{
public:
Application( int argc, char * argv[]):QApplication(argc,argv){}
protected:
virtual bool event(QEvent * event )
{
if( event->type() == QEvent::FileOpen )
{
QMessageBox box;
box.setText(static_cast<QFileOpenEvent>(event)->file());
box.exec();
}
return QApplication::event(event);
}

}