[Qt-interest] Executing code after QApplication::exec() without user intervention

Nikos Chantziaras realnc at arcor.de
Wed Apr 28 14:02:24 CEST 2010


On 04/27/2010 10:15 AM, Mark Summerfield wrote:
> On 2010-04-27, Nikos Chantziaras wrote:
>> [...]
>> How do have a method executed automatically after the main event loop
>> (app->exec()) has started?  This is for the case where the application
>> takes a command-line argument to open a file.  Right now, this is simply
>> done the usual way, through the application's "File->Open" menu.  But
>> providing a filename as a command-line arg means I need to open the file
>> without user intervention.
> [...]
> Then in the main window's constructor, at the end I have something like:
>
> 	if (!filename.isEmpty())
> 	    QTimer::singleShot(0, this, SLOT(loadFile()));
> 	    // this assumes that filename is a member variable; if not
> 	    // use:
> 	    // QMetaObject::invokeMethod(this, "loadFile",
> 	    //	    Qt::QueuedConnection,
> 	    //	    Q_ARG(QString, filename));
>
> This assumes you have a method with signature
>      void loadFile(); // if you use the timer, or
>      void loadFile(const QString&filename); // if you use invokeMethod
>[...]

Thanks, Mark, and everyone else for your suggestions.  The QTimer and 
QMetaObject way both work nicely and look like exactly what I need.



More information about the Qt-interest-old mailing list